diff --git a/plugs/plug-manager/plugmanager.test.ts b/plugs/plug-manager/plugmanager.test.ts index 1bcddbcd..b0e61ca8 100644 --- a/plugs/plug-manager/plugmanager.test.ts +++ b/plugs/plug-manager/plugmanager.test.ts @@ -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)); @@ -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)); diff --git a/plugs/plug-manager/plugmanager.ts b/plugs/plug-manager/plugmanager.ts index 3b55c8c2..4837a8ed 100644 --- a/plugs/plug-manager/plugmanager.ts +++ b/plugs/plug-manager/plugmanager.ts @@ -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 = @@ -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