Skip to content

Commit

Permalink
feat(Codeblock): ✨ Add fields field to limit codeblock tree to only…
Browse files Browse the repository at this point in the history
… show given fields
  • Loading branch information
SkepticMystic committed Apr 18, 2022
1 parent 8a83dbf commit d6fedda
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
20 changes: 11 additions & 9 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand All @@ -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 });
Expand Down Expand Up @@ -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}
*/
Expand All @@ -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*)/);
Expand Down
39 changes: 23 additions & 16 deletions src/Codeblocks.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -9,13 +10,16 @@ import { dropFolder, splitAndTrim } from "./Utils/generalUtils";
import {
dfsAllPaths,
getReflexiveClosure,
getSubForFields,
getSubInDirs,
} from "./Utils/graphUtils";
import { getFieldInfo, getFields, getOppDir } from "./Utils/HierUtils";
import { createJuggl } from "./Visualisations/Juggl";

export function getCodeblockCB(plugin: BCPlugin) {
const { settings, db } = plugin;
const { userHiers } = settings;

return (
source: string,
el: HTMLElement,
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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}
*/
Expand All @@ -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) {
Expand Down Expand Up @@ -193,15 +201,14 @@ function codeblockError(plugin: BCPlugin, parsedSource: ParsedCodeblock) {
<pre><code>
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
</code></pre>`;
}
Expand Down

0 comments on commit d6fedda

Please sign in to comment.