From b23c062cb13994a4267b2f160a487f51102cab02 Mon Sep 17 00:00:00 2001 From: jensmtg Date: Wed, 13 Jul 2022 04:11:10 +0200 Subject: [PATCH] Enable full note reparse, and first heading/attribute title --- src/main.tsx | 29 ++++++++++++++++++---- src/utils.tsx | 66 ++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 74 insertions(+), 21 deletions(-) diff --git a/src/main.tsx b/src/main.tsx index 0397137..3477355 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -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'; @@ -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

: + 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) )) - } })) } @@ -46,7 +51,12 @@ const ReactApp = (props: any) => { {components.map((p: any) => { return

-

{p.fileName}

+

{p.fileName}   + +

{p.inner.map((div: any, i: number) => (
{ } -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; @@ -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(); @@ -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) diff --git a/src/utils.tsx b/src/utils.tsx index 3d1919e..dd0e15d 100644 --- a/src/utils.tsx +++ b/src/utils.tsx @@ -1,5 +1,7 @@ const SHOW_ANCESTORS = true +const FRONTMATTER_KEY = 'tittel' + export const getInlinkedPages = (dv: any, currentPage: any) => { return currentPage.file.inlinks @@ -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) => { @@ -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) { @@ -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 + } })); @@ -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 = (() => {