From d6fedda7d740019123265eb9ef340d44877f36e2 Mon Sep 17 00:00:00 2001 From: Ross Keenan Date: Mon, 18 Apr 2022 13:02:58 +0200 Subject: [PATCH] feat(Codeblock): :sparkles: Add `fields` field to limit codeblock tree to only show given fields --- main.js | 20 +++++++++++--------- src/Codeblocks.ts | 39 +++++++++++++++++++++++---------------- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/main.js b/main.js index 20aff296..c2866798 100644 --- a/main.js +++ b/main.js @@ -37320,17 +37320,19 @@ class CBTree extends SvelteComponent { function getCodeblockCB(plugin) { const { settings, db } = plugin; + const { userHiers } = settings; return (source, el, ctx) => { - var _a, _b; + var _a; db.start2G("Codeblock"); const parsedSource = parseCodeBlockSource(source); const err = codeblockError(plugin, parsedSource); if (err !== "") { el.innerHTML = err; + db.end2G(); return; } let min = 0, max = Infinity; - let { depth, dir, from, implied, flat } = parsedSource; + let { depth, dir, fields, from, implied, flat } = parsedSource; if (depth !== undefined) { const minNum = parseInt(depth[0]); if (!isNaN(minNum)) @@ -37340,14 +37342,13 @@ function getCodeblockCB(plugin) { max = maxNum; } const currFile = plugin.app.metadataCache.getFirstLinkpathDest(ctx.sourcePath, ""); - const { userHiers } = settings; const { basename } = currFile; let froms = undefined; if (from !== undefined) { try { - const api = (_a = plugin.app.plugins.plugins.dataview) === null || _a === void 0 ? void 0 : _a.api; + const api = getDVApi(plugin); if (api) { - const pages = (_b = api.pagePaths(from)) === null || _b === void 0 ? void 0 : _b.values; + const pages = (_a = api.pagePaths(from)) === null || _a === void 0 ? void 0 : _a.values; froms = pages.map(dropFolder); } else @@ -37362,7 +37363,8 @@ function getCodeblockCB(plugin) { ? getSubInDirs(plugin.mainG, dir) : getSubInDirs(plugin.mainG, dir, oppDir); const closed = getReflexiveClosure(sub, userHiers); - const subClosed = getSubInDirs(closed, dir); + const subFields = fields !== null && fields !== void 0 ? fields : getFields(userHiers); + const subClosed = getSubForFields(getSubInDirs(closed, dir), subFields); const allPaths = dfsAllPaths(subClosed, basename); const index = createIndex(allPaths, false); loglevel.info({ allPaths, index }); @@ -37391,7 +37393,7 @@ function getCodeblockCB(plugin) { }; } /** - * Parse a string as a boolean value. + * Parse a string as a boolean value. If not "true" or "false", return `value`. * @param {string} value - string * @returns {string | boolean} */ @@ -37408,8 +37410,8 @@ function parseCodeBlockSource(source) { const value = getValue(field); results[field] = parseAsBool(value); }); - results.field = results.field - ? splitAndTrim(results.field) + results.fields = results.fields + ? splitAndTrim(results.fields) : undefined; if (results.depth) { const match = results.depth.match(/(\d*)-?(\d*)/); diff --git a/src/Codeblocks.ts b/src/Codeblocks.ts index dc1ba7be..1f48244c 100644 --- a/src/Codeblocks.ts +++ b/src/Codeblocks.ts @@ -1,5 +1,6 @@ import { info } from "loglevel"; import { MarkdownPostProcessorContext, Notice } from "obsidian"; +import { getDVApi } from "./Utils/ObsidianUtils"; import { createIndex, indexToLinePairs } from "./Commands/CreateIndex"; import CBTree from "./Components/CBTree.svelte"; import { CODEBLOCK_FIELDS, CODEBLOCK_TYPES, DIRECTIONS } from "./constants"; @@ -9,6 +10,7 @@ import { dropFolder, splitAndTrim } from "./Utils/generalUtils"; import { dfsAllPaths, getReflexiveClosure, + getSubForFields, getSubInDirs, } from "./Utils/graphUtils"; import { getFieldInfo, getFields, getOppDir } from "./Utils/HierUtils"; @@ -16,6 +18,8 @@ import { createJuggl } from "./Visualisations/Juggl"; export function getCodeblockCB(plugin: BCPlugin) { const { settings, db } = plugin; + const { userHiers } = settings; + return ( source: string, el: HTMLElement, @@ -30,9 +34,10 @@ export function getCodeblockCB(plugin: BCPlugin) { db.end2G(); return; } + let min = 0, max = Infinity; - let { depth, dir, from, implied, flat } = parsedSource; + let { depth, dir, fields, from, implied, flat } = parsedSource; if (depth !== undefined) { const minNum = parseInt(depth[0]); if (!isNaN(minNum)) min = minNum; @@ -44,13 +49,12 @@ export function getCodeblockCB(plugin: BCPlugin) { ctx.sourcePath, "" ); - const { userHiers } = settings; const { basename } = currFile; let froms = undefined; if (from !== undefined) { try { - const api = plugin.app.plugins.plugins.dataview?.api; + const api = getDVApi(plugin); if (api) { const pages = api.pagePaths(from)?.values; froms = pages.map(dropFolder); @@ -66,7 +70,10 @@ export function getCodeblockCB(plugin: BCPlugin) { ? getSubInDirs(plugin.mainG, dir) : getSubInDirs(plugin.mainG, dir, oppDir); const closed = getReflexiveClosure(sub, userHiers); - const subClosed = getSubInDirs(closed, dir); + + const subFields = fields ?? getFields(userHiers); + const subClosed = getSubForFields(getSubInDirs(closed, dir), subFields); + const allPaths = dfsAllPaths(subClosed, basename); const index = createIndex(allPaths, false); @@ -109,7 +116,7 @@ export function getCodeblockCB(plugin: BCPlugin) { } /** - * Parse a string as a boolean value. + * Parse a string as a boolean value. If not "true" or "false", return `value`. * @param {string} value - string * @returns {string | boolean} */ @@ -126,13 +133,14 @@ function parseCodeBlockSource(source: string): ParsedCodeblock { const results: { [field in CodeblockFields]: string | boolean | string[] } = {}; + CODEBLOCK_FIELDS.forEach((field) => { const value = getValue(field); results[field] = parseAsBool(value); }); - results.field = results.field - ? splitAndTrim(results.field as string) + results.fields = results.fields + ? splitAndTrim(results.fields as string) : undefined; if (results.depth) { @@ -193,15 +201,14 @@ function codeblockError(plugin: BCPlugin, parsedSource: ParsedCodeblock) {

       type: tree
       dir: ${validDir ? dir : "down"}
-      fields: ${
-        allFields
-          .map((f) => {
-            return { f, dir: getFieldInfo(userHiers, f).fieldDir };
-          })
-          .filter((info) => info.dir === dir)
-          .map((info) => info.f)
-          .join(", ") || "child"
-      }
+      fields: ${allFields
+      .map((f) => {
+        return { f, dir: getFieldInfo(userHiers, f).fieldDir };
+      })
+      .filter((info) => info.dir === dir)
+      .map((info) => info.f)
+      .join(", ") || "child"
+    }
       depth: 3
       
`; }