Skip to content

Instantly share code, notes, and snippets.

@crisrojas
Last active February 16, 2023 22:04
Show Gist options
  • Save crisrojas/890016c99482a4cbd03186508cf9dee7 to your computer and use it in GitHub Desktop.
Save crisrojas/890016c99482a4cbd03186508cf9dee7 to your computer and use it in GitHub Desktop.
Nested hashtag parser
// Used in my BearKit Project: https://notas.cristian.lat
// Attribution not required, but highly appreciated 🙏
class Tag {
constructor(name, slug, children) {
this.name = name
this.slug = slug
this.children = children
this.type = "tag"
}
addChildren = (string, originalString) => {
var childrenArray = string.split("/")
const index = this.children.findIndex(object => {
return object.name === childrenArray[0];
});
// If doesn't exists, push to array
if (index == -1) {
let tag = tagMaker(string, originalString)
this.children.push(tag)
return
}
let existingChild = this.children[index]
// If exists, traverse it
while (childrenArray.length > 0) {
let first = childrenArray.shift()
const index = existingChild.children.findIndex(object => {
return object.name === childrenArray[0];
});
if (index == -1) {
let string = childrenArray.join("/")
let tag = tagMaker(string, originalString)
existingChild.children.push(tag)
break
}
}
this.children[index] = existingChild
}
}
const tagMaker = (name, originalString) => {
var array = name.split("/")
var tag;
while (array.length > 0) {
let slug = array.join('/')
let last = array.pop()
if (originalString) {
slug = originalString
}
if (tag === undefined) {
tag = new Tag(last, slug, [])
} else {
tag = new Tag(last, slug, [tag])
}
}
return tag
}
export const tagTreeMaker = (tags) => {
var tagTree = []
tags.forEach(string => {
let tagArray = string.split("/")
let firstLevelname = tagArray.shift()
let subtags = tagArray.join("/")
const index = tagTree.findIndex(object => {
return object.name === firstLevelname
})
if (index == -1) {
let tag = tagMaker(string)
tagTree.push(tag)
return
}
tagTree[index].addChildren(subtags, string)
})
return tagTree
}
let tagArray = [
"aprendizaje",
"bear",
"biología",
"cero-resistencia",
"chunking",
"citas",
"comunicación",
"concepto",
"constancia",
"cristianismo",
"eficacia",
"embudo",
"emociones",
"enfoque",
"humildad",
"ideas",
"inbox",
"inbox/clarificar",
"inbox/contradicción",
"inbox/desarrollar",
"inbox/repetida",
"inbox/verificar",
"listas",
"minimalismo",
"movimiento",
"paciencia",
"política",
"preguntas",
"presencia",
"prioridad",
"privado",
"programación",
"programación/algoritmica",
"programación/hugo",
"programación/swift",
"proyectos",
"publicables",
"referencias",
"registro",
"registro/diario",
"registro/logros",
"registro/misc",
"registro/proyectos",
"repetición",
"responsabilidad",
"restricciones",
"revisar-cada-día",
"simplicidad",
"sufrimiento",
"thearchive",
"unidad",
"valentía"
]
let tagTree = tagTreeMaker(tagArray).filter(e=> e.children.length > 0)
let json = JSON.stringify(tagTree, null, 2)
console.log(json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment