Fix index range for data, space-lua and space-style blocks
This commit is contained in:
@@ -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");
|
||||
});
|
||||
});
|
||||
|
||||
+3
-2
@@ -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,
|
||||
|
||||
@@ -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("```");
|
||||
}
|
||||
});
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
|
||||
@@ -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("```");
|
||||
}
|
||||
});
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user