Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/data-import/inline-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,30 @@ function findClosing(

/** Find the '::' separator in an inline field. */
function findSeparator(line: string, start: number): { key: string; valueIndex: number } | undefined {
// lookup for inline code blocks on that line.
let code_blocks: [number, number][] = []
for (let start = line.indexOf("`"); (start < line.length) && (start >= 0); start = line.indexOf("`", start)) {
// ignore escaped back ticks.
if (line.charAt(start - 1) == "\\") {
start = start + 1;
continue;
}
let end = line.indexOf("`", start + 1);
code_blocks.push([start, end]);
if (end != -1) {
start = end + 1;
} else {
break;
}
}

let sep = line.indexOf("::", start);
if (sep < 0) return undefined;
for (const pair of code_blocks) {
if ((pair[0] < sep) && ((sep < pair[1]) || (pair[1] == -1))) {
return undefined;
}
}

return { key: line.substring(start, sep).trim(), valueIndex: sep + 2 };
}
Expand Down
41 changes: 41 additions & 0 deletions src/test/parse/parse.inline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ describe("Simple Inline Inline", () => {
test("Incorrect Brackets", () => expect(extractInlineFields("[key:value]")).toEqual([]));
test("Incorrect Parenthesis", () => expect(extractInlineFields("(key:value)")).toEqual([]));

test("No Field, Code block between Parenthesis", () => expect(extractInlineFields("(Some `code::inline::codeblock`)")).toEqual([]));
test("No Field, multiple Code block between Parenthesis", () =>
expect(extractInlineFields("(Some `codeblock` and `another::one`)")).toEqual([])
);
test("No Field, Parenthesis inside code block", () => expect(extractInlineFields("Some `(inline::codeblock)`")).toEqual([]));
test("No Field, Open Parenthesis in Code block, close outside", () => expect(extractInlineFields("Some `(inline::codeblock`)")).toEqual([]));
test("No Field, Open Parenthesis outside Code block, close inside", () => expect(extractInlineFields("(Some `inline::codebl)ock`")).toEqual([]));

test("Trivial Brackets", () =>
expect(extractInlineFields("[key::value]")).toEqual([
{
Expand Down Expand Up @@ -83,6 +91,39 @@ describe("Simple Inline Inline", () => {
wrapping: "[",
});
});

test("One Field after a codeblock between parenthesis", () =>
expect(extractInlineFields("(Some `inline::codeblock`) (key::value)")).toEqual([{
key: "key",
value: "value",
start: 27,
startValue: 33,
end: 39,
wrapping: "(",
}
]));

test("One Field before a codeblock between parenthesis", () =>
expect(extractInlineFields("Some (key::value) text (Some `inline::codeblock`)")).toEqual([{
key: "key",
value: "value",
start: 5,
startValue: 11,
end: 17,
wrapping: "(",
}
]));
test("Escaped back tick outside a code block don’t count as code block start.", () =>
expect(extractInlineFields("(some \\` escaped::backtick)")).toEqual([{
key: "some \\` escaped",
value: "backtick",
start: 0,
startValue: 18,
end: 27,
wrapping: "(",
}
]))

});

describe("Inline Inline Edge Cases", () => {
Expand Down