diff --git a/plugs/index/data.test.ts b/plugs/index/data.test.ts index 9c26fe8a..0bbd9a49 100644 --- a/plugs/index/data.test.ts +++ b/plugs/index/data.test.ts @@ -108,3 +108,37 @@ describe("$ref anchor in fenced data blocks", () => { expect("$ref" in person).toBe(false); }); }); + +describe("range covers the YAML content, not the fence markers", () => { + test("single-doc block: range bounds the inner YAML exactly", async () => { + createMockSystem(); + const content = "name: Pete\nage: 100"; + const md = `\`\`\`#person\n${content}\n\`\`\``; + const results = await indexDataForTest(md, "Page"); + const person = results.find((o) => o.tag === "person")!; + const [from, to] = person.range as [number, number]; + expect(md.slice(from, to)).toBe(content); + // sanity: not picking up the fence markers + expect(md.slice(from, to)).not.toContain("```"); + }); + + test("multi-doc block: doc 1 starts after doc 0's end + separator length", async () => { + createMockSystem(); + const md = [ + "```#person", + "name: Pete", + "age: 100", + "---", + "name: Hank", + "age: 101", + "```", + ].join("\n"); + const results = await indexDataForTest(md, "Page"); + const persons = results.filter((o) => o.tag === "person"); + expect(persons).toHaveLength(2); + const [a, b] = persons; + expect(b.range[0]).toBe(a.range[1] + "---".length); + expect(md.slice(a.range[0], a.range[1])).toContain("Pete"); + expect(md.slice(b.range[0], b.range[1])).toContain("Hank"); + }); +}); diff --git a/plugs/index/data.ts b/plugs/index/data.ts index 5b10b89f..f7458d6a 100644 --- a/plugs/index/data.ts +++ b/plugs/index/data.ts @@ -46,11 +46,12 @@ export function indexData( const codeText = codeTextNode.children![0].text!; const dataType = fenceType === "data" ? "data" : fenceType.substring(1); try { + const codeFrom = codeTextNode.from!; const docs = codeText.split(separator); let cursor = 0; // We support multiple YAML documents in one block for (let i = 0; i < docs.length; i++) { - const docStart = t.from! + cursor; + const docStart = codeFrom + cursor; const docEnd = docStart + docs[i].length; const doc = YAML.load(docs[i]); if (!doc) { @@ -81,7 +82,7 @@ export function indexData( }; updateITags(dataObj, frontmatter); dataObjects.push(dataObj); - cursor += docs[i].length; + cursor += docs[i].length + separator.length; } tagObjects.set(dataType, { ref: dataType, diff --git a/plugs/index/space_lua.test.ts b/plugs/index/space_lua.test.ts index 66b58a92..b24ebd3b 100644 --- a/plugs/index/space_lua.test.ts +++ b/plugs/index/space_lua.test.ts @@ -38,4 +38,11 @@ test("Test space lua indexing", async () => { expect(objects.length).toEqual(2); expect(objects[0].priority).toEqual(undefined); expect(objects[1].priority).toEqual(10); + + // range covers the inner code text, not the ``` fences + for (const o of objects) { + const [from, to] = o.range as unknown as [number, number]; + expect(testPage.slice(from, to)).toBe(o.script); + expect(testPage.slice(from, to)).not.toContain("```"); + } }); diff --git a/plugs/index/space_lua.ts b/plugs/index/space_lua.ts index 5a12547b..c497e18d 100644 --- a/plugs/index/space_lua.ts +++ b/plugs/index/space_lua.ts @@ -41,7 +41,7 @@ export function indexSpaceLua( allSpaceLuas.push({ ref: `${pageMeta.name}@${t.from!}`, tag: "space-lua", - range: [t.from!, t.to!], + range: [codeTextNode.from!, codeTextNode.to!], script: codeText, priority: priority !== undefined ? +priority : undefined, }); diff --git a/plugs/index/space_style.test.ts b/plugs/index/space_style.test.ts index 94228771..35a72e9e 100644 --- a/plugs/index/space_style.test.ts +++ b/plugs/index/space_style.test.ts @@ -36,4 +36,11 @@ test("Test space style indexing", async () => { expect(objects.length).toEqual(2); expect(objects[0].priority).toEqual(undefined); expect(objects[1].priority).toEqual(10); + + // range covers the inner code text, not the ``` fences + for (const o of objects) { + const [from, to] = o.range as unknown as [number, number]; + expect(testPage.slice(from, to)).toBe(o.style); + expect(testPage.slice(from, to)).not.toContain("```"); + } }); diff --git a/plugs/index/space_style.ts b/plugs/index/space_style.ts index 41f3d5b2..63a14fee 100644 --- a/plugs/index/space_style.ts +++ b/plugs/index/space_style.ts @@ -46,7 +46,7 @@ export function indexSpaceStyle( allStyles.push({ ref: `${pageMeta.name}@${t.from!}`, tag: "space-style", - range: [t.from!, t.to!], + range: [codeTextNode.from!, codeTextNode.to!], style: codeText, priority: priority !== undefined ? +priority : undefined, }); diff --git a/website/CHANGELOG.md b/website/CHANGELOG.md index 5e14ff8e..dcb9f9d8 100644 --- a/website/CHANGELOG.md +++ b/website/CHANGELOG.md @@ -4,6 +4,8 @@ An attempt at documenting the changes/new features introduced in each release. Whenever a commit is pushed to the `main` branch, within ~5 minutes, it will be released as a docker image with the `:v2` tag, and a binary in the [edge release](https://github.com/silverbulletmd/silverbullet/releases/tag/edge). If you want to live on the bleeding edge of SilverBullet goodness (or regression) this is where to do it. * [[X-Ray]]: an editor lens (run `Editor: Toggle X-Ray`) to inspect indexed objects inline. +* Fix: indexed `range` for `data` blocks (and `#tag` data blocks) now points at the YAML content instead of the surrounding fence markers; multi-document blocks now compute per-doc ranges correctly. +* Fix: indexed `range` for `space-lua` and `space-style` blocks now points at the inner code, not the ` ``` ` fences. * Fix: [[Page Template]]s are now fixed (required specifying of `suggestedName` before), now also supports objects as frontmatter ## 2.7.0