Skip to content

Commit

Permalink
Insert Plug URI into page
Browse files Browse the repository at this point in the history
  • Loading branch information
Maarrk committed Nov 27, 2024
1 parent 6951726 commit 3ba1d2c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
4 changes: 2 additions & 2 deletions plugs/plug-manager/plugmanager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ plugs:
\`\`\`space-config
plugs:
- test:old.plug.js
- test:my.plug.js
# some comment
- test:my.plug.js
\`\`\`
`;
after = replaceRanges(before, insertIntoPlugPage("test:my.plug.js", before));
Expand All @@ -102,7 +102,7 @@ plugs: [ "test:old.plug.js" ]
`;
expected = `I love square brackets
\`\`\`space-config
plugs: [ "test:old.plug.js", "test:my.plug.js" ]
plugs: [ "test:old.plug.js" , "test:my.plug.js" ]
\`\`\`
`;
after = replaceRanges(before, insertIntoPlugPage("test:my.plug.js", before));
Expand Down
44 changes: 42 additions & 2 deletions plugs/plug-manager/plugmanager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { parseMarkdown } from "$common/markdown_parser/parser.ts";
import { addParentPointers } from "@silverbulletmd/silverbullet/lib/tree";
import { findNodeOfType } from "@silverbulletmd/silverbullet/lib/tree";
import { assert } from "@std/assert/assert";
import { builtinLanguages } from "$common/languages.ts";

const plugsPage = "PLUGS";
const plugsPrelude =
Expand Down Expand Up @@ -217,13 +218,52 @@ export function insertIntoPlugPage(
} else if (configInfo) {
assert(configInfo.parent);
const configText = findNodeOfType(configInfo.parent, "CodeText");
assert(configText && configText.children);
assert(configText.children.length === 1 && configText.children[0].text);
assert(configText && configText.from && configText.to);
assert(configText.children?.length === 1 && configText.children[0].text);
const config = configText.children[0].text;
// Prepend right after `plugs` key
// otherwise we'd have to rewrite all traversal functions from lib/tree for lezer's YAML parser

// TODO: find "plugs:\n" or "plugs:\s[" in text and calculate the edit position
const configTree = builtinLanguages["yaml"].parser.parse(config);
configTree.iterate({
enter: (n) => {
if (
n.name === "Document" &&
config.substring(n.from, n.to).startsWith("plugs:")
) {
assert(configText.from);
if (
n.node.lastChild &&
config.substring(n.node.lastChild.from, n.node.lastChild.to) === "]"
) {
// This is a list with square brackets
edits.push({
from: configText.from + n.node.lastChild.from,
to: configText.from + n.node.lastChild.from,
text: `, "${uri}" `,
});
} else {
edits.push({
from: configText.from + n.to,
to: configText.from + n.to,
text: `\n- ${uri}`,
});
}
return false; // no need to traverse any more
} else {
return true;
}
},
});
if (edits.length === 0) {
// No plugs in this block
edits.push({
from: configText.to,
to: configText.to,
text: `\nplugs:\n- ${uri}`,
});
}
} else {
// Just add the whole block if there's nothing
const configBlock = `\`\`\`space-config
Expand Down

0 comments on commit 3ba1d2c

Please sign in to comment.