Skip to content

Commit

Permalink
Enable full note reparse, and first heading/attribute title
Browse files Browse the repository at this point in the history
  • Loading branch information
jensmtg committed Jul 13, 2022
1 parent fea82d2 commit b23c062
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 21 deletions.
29 changes: 25 additions & 4 deletions src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Plugin, Vault, Workspace, MarkdownView, MarkdownRenderer, MarkdownPostProcessorContext } from 'obsidian';
import { Plugin, Vault, Workspace, MarkdownView, MarkdownRenderer, MarkdownPostProcessorContext, PluginSettingTab, Setting } from 'obsidian';
import * as React from "react";
import { createRoot } from "react-dom/client";
import { getInlinkedPages, makeSubtrees } from './utils';
Expand All @@ -19,11 +19,16 @@ const ReactApp = (props: any) => {

const renderAllMarkdownBlocks = async () => {
return await Promise.all(data.map(async (p: any) => {

// Parse title, and strip innerHTML of enclosing <p>:
const titleAsMd = await renderMarkdownBlock(p.title)
const titleInnerHTML = titleAsMd.innerHTML.slice(3, -4)

return {
...p,
titleInnerHTML: titleInnerHTML,
inner: await Promise.all(p.reparse.map(async (reparse: string) => await renderMarkdownBlock(reparse)
))

}
}))
}
Expand All @@ -46,7 +51,12 @@ const ReactApp = (props: any) => {
{components.map((p: any) => {

return <div key={p.fileName}>
<h2>{p.fileName}</h2>
<h2>{p.fileName} &nbsp;
<span
style={{opacity: 0.5}}
dangerouslySetInnerHTML={{ __html: p.titleInnerHTML }}
/>
</h2>
{p.inner.map((div: any, i: number) => (
<div
key={i}
Expand All @@ -67,9 +77,17 @@ const ReactApp = (props: any) => {
}


export default class ObsidianInflux extends Plugin {
interface MyPluginSettings {
mySetting: string;
}

// const DEFAULT_SETTINGS: MyPluginSettings = {
// mySetting: 'default'
// }

export default class ObsidianInflux extends Plugin {

settings: MyPluginSettings;
vault: Vault;
workspace: Workspace;
data: any;
Expand Down Expand Up @@ -111,6 +129,8 @@ export default class ObsidianInflux extends Plugin {
const inlinkedPages = getInlinkedPages(this.dv, page)
const data = await makeSubtrees(this.dv, page, inlinkedPages)



const div = el.createEl("div");
this.anchor = createRoot(div)
this.anchor.render(<ReactApp data={data} view={activeView} />);
Expand All @@ -127,6 +147,7 @@ export default class ObsidianInflux extends Plugin {
if (el && ctx.sourcePath && this.dv) {

const page = await this.dv.page(ctx.sourcePath)

const inlinkedPages = getInlinkedPages(this.dv, page)
const data = await makeSubtrees(this.dv, page, inlinkedPages)

Expand Down
66 changes: 49 additions & 17 deletions src/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

const SHOW_ANCESTORS = true
const FRONTMATTER_KEY = 'tittel'


export const getInlinkedPages = (dv: any, currentPage: any) => {
return currentPage.file.inlinks
Expand All @@ -9,7 +11,7 @@ export const getInlinkedPages = (dv: any, currentPage: any) => {

export const makeSubtrees = async (dv: any, currentPage: any, inlinkedPages: any) => {

const ret: any = []
const ret: any = []

await Promise.all(inlinkedPages.map(async (p: any, i: number) => {

Expand All @@ -23,11 +25,16 @@ export const makeSubtrees = async (dv: any, currentPage: any, inlinkedPages: any
const file = await dv.app.vault.getAbstractFileByPath(p.file.path)
const content = await dv.app.vault.read(file)

console.log('curr', currentPage)
const metadata = dv.app.metadataCache.getFileCache(file);
const titleByFrontmatterAttribute = metadata?.frontmatter?.[FRONTMATTER_KEY]
const titleByFirstHeader = metadata?.headings?.[0]?.heading
const title = titleByFrontmatterAttribute || titleByFirstHeader || ''

const searchStrings = [asLink(currentPage.file.name)]
const tree = doTree(content)

console.log('tree', tree)


// Add any aliases to the array of ids to search for
if (currentPage.alias) {
Expand All @@ -42,23 +49,41 @@ export const makeSubtrees = async (dv: any, currentPage: any, inlinkedPages: any

console.log('searchstrings', searchStrings)

findSubtree(tree, searchStrings, saveSubtree, [])

if (subtrees) {
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)
}
})
}

ret[i] = {
fileName: p.file.name,
reparse: reparse
title: title,
}


if (searchStrings.some(str => title.includes(str))) {

let fullReparse = ''
for (let i = 0; i < tree.length; i++) {
doReparseFullNote(tree[i], 0, (text: string) => {
fullReparse = fullReparse + text
})
}
ret[i].reparse = [fullReparse]

}
else {

findSubtree(tree, searchStrings, saveSubtree, [])

if (subtrees) {
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)
}
})
}

ret[i].reparse = reparse

}

}));
Expand Down Expand Up @@ -99,6 +124,13 @@ const doReparse = (node: any, level: number, saveReparse: (text: string, index:
node.content.forEach((child: any) => doReparse(child, level + 1, saveReparse, index))
}

const doReparseFullNote = (node: any, level: number, saveReparse: (text: string) => void) => {
saveReparse(`${' '.repeat(2 * level)}${node.root}\n`)
node.content.forEach((child: any) => doReparseFullNote(child, level + 1, saveReparse))
}



// https://stackoverflow.com/a/70338823
const indentation = (() => {

Expand Down

0 comments on commit b23c062

Please sign in to comment.