Skip to content

Instantly share code, notes, and snippets.

@onliniak
Created December 10, 2020 21:54
Show Gist options
  • Save onliniak/6578f582e57f1939e89d9f194e5a68c7 to your computer and use it in GitHub Desktop.
Save onliniak/6578f582e57f1939e89d9f194e5a68c7 to your computer and use it in GitHub Desktop.
1.5KB YAML to menu
const str =
`Category1:
name1: link1
name2: link2
name3: link3
name4: link4
Category2:
another_name1: betterLink
another_name2: betterLink2
another_name3: betterLink3`
const regexp = /(?<category>\w*):\s*((?<a>\w*):\s+(?<href>\w*))*(\w*)*/g
const array = [...str.matchAll(regexp)]
const district = /^\S$|^\S*\S$/gm
const str2 = str.replaceAll(/:/g, '')
const array2 = [...str2.matchAll(district)]
let i, g, a, r
r = {}
function dynamicArrayVariables(variable) {
window[variable] = []
}
array2.forEach(dynamicArrayVariables)
for (i = 0; i < array.length; i++) {
if (typeof array[i][5] === 'undefined')
g = i
if (typeof array[i][3] != 'undefined') {
let key = array[i][3]
window[array[g][1]].push({
[key]: array[i][4]
})
}
if (typeof array[i][3] === 'undefined')
if (array[i][1] != array[i][3]) {
let key = array[i][1]
window[array[g][1]].push({
[key]: array[i][5]
})
}
}
const links = str.toString()
for (i = 0; i < array2.length; i++) {
const length = window[array2[i]].length
for (a = 0; a < length; a++) {
const regex = new RegExp(`${array2[i].toString()}:(\\s*(\\w*):\\s*(\\w*)){${a+1}}`, "g")
const found = [...links.matchAll(regex)]
mainCategory = array2[i].toString()
r[`${mainCategory}Links`] += `${found[0][3]}@`
r[`${mainCategory}Names`] += `${found[0][2]}@`
}
arrayNames = r[`${mainCategory}Names`]
.replace("undefined", "")
.split("@")
arrayLinks = r[`${mainCategory}Links`]
.replace("undefined", "")
.split("@")
div = document.createElement('div')
div.id = mainCategory
document.body.appendChild(div);
for (c = 0; c < length; c++) {
link = document.createElement('a')
link.setAttribute('href', arrayLinks[c])
link.textContent = arrayNames[c]
document.getElementById(mainCategory).appendChild(link);
}
}
@onliniak
Copy link
Author

Generates menus from YAML files, inspired by textile.

Poorly optimized, so if you have too much free time, please fork me ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment