Skip to content

Commit

Permalink
Enable ancestors
Browse files Browse the repository at this point in the history
  • Loading branch information
jensmtg committed Jun 27, 2022
1 parent 34a3ce2 commit fea82d2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

const SHOW_ANCESTORS = true

export const getInlinkedPages = (dv: any, currentPage: any) => {
return currentPage.file.inlinks
.map((link: any) => dv.page(link.path))
Expand All @@ -15,7 +17,7 @@ export const makeSubtrees = async (dv: any, currentPage: any, inlinkedPages: any
const reparse: string[] = []
//let reparse = ''

const saveSubtree = (tree: any) => { subtrees.push(tree) }
const saveSubtree = (tree: any, ancestors: any[]) => { subtrees.push([tree, ancestors]) }
const saveReparse = (text: string, index: number) => { reparse[index] = (reparse[index] || '') + text }

const file = await dv.app.vault.getAbstractFileByPath(p.file.path)
Expand All @@ -40,11 +42,17 @@ export const makeSubtrees = async (dv: any, currentPage: any, inlinkedPages: any

console.log('searchstrings', searchStrings)

findSubtree(tree, searchStrings, saveSubtree)
findSubtree(tree, searchStrings, saveSubtree, [])

if (subtrees) {
subtrees.forEach((subtree, i) => {
doReparse(subtree, 1, saveReparse, i)
subtrees.forEach(([subtree, ancestors], i) => {
if (SHOW_ANCESTORS) {
doReparseAncestors(ancestors, 0, saveReparse, i)
doReparse(subtree, ancestors.length, saveReparse, i)
}
else {
doReparse(subtree, 0, saveReparse, i)
}
})
}

Expand All @@ -60,20 +68,32 @@ export const makeSubtrees = async (dv: any, currentPage: any, inlinkedPages: any



const findSubtree = (nodes: any, searchStrings: string[], saveSubtree: (tree: any) => void) => {
const findSubtree = (nodes: any, searchStrings: string[], saveSubtree: (tree: any, ancestors: any[]) => void, ancestors: any[]) => {
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i]
if (searchStrings.some(str => node.root.includes(str))) {
// subtree = node
saveSubtree(node)
saveSubtree(node, ancestors)
}
else if (node.content?.length > 0) {
findSubtree(node.content, searchStrings, saveSubtree)
findSubtree(node.content, searchStrings, saveSubtree, [...ancestors, node])
}
}
}


const doReparseAncestors = (ancestors: any[], level: number, saveReparse: (text: string, index: number) => void, index: number) => {
ancestors.forEach((node: any, j: number) => {
const _level = level + j
// console.log('n', node.root.substring(0, 2))
const expectedBullet = node.root.substring(0, 2)
const nodeString = expectedBullet === '* '
? `* <span class="ancestor">${node.root.slice(2)}</span>`
: node.root
saveReparse(`${' '.repeat(2 * _level)}${nodeString}\n`, index)
})
}

const doReparse = (node: any, level: number, saveReparse: (text: string, index: number) => void, index: number) => {
saveReparse(`${' '.repeat(2 * level)}${node.root}\n`, index)
node.content.forEach((child: any) => doReparse(child, level + 1, saveReparse, index))
Expand Down
4 changes: 4 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@
.block-language-influx {
color: 'red'
}

.ancestor {
color: #bbb;
}

0 comments on commit fea82d2

Please sign in to comment.