Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't replace block IDs in codeblocks #596

Merged
merged 4 commits into from
Jun 11, 2024
Merged
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
17 changes: 2 additions & 15 deletions src/compiler/GardenPageCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
import Logger from "js-logger";
import { DataviewCompiler } from "./DataviewCompiler";
import { PublishFile } from "../publishFile/PublishFile";
import { replaceBlockIDs } from "./replaceBlockIDs";

export interface Asset {
path: string;
Expand Down Expand Up @@ -144,21 +145,7 @@ export class GardenPageCompiler {
};

createBlockIDs: TCompilerStep = () => (text: string) => {
const block_pattern = / \^([\w\d-]+)/g;
const complex_block_pattern = /\n\^([\w\d-]+)\n/g;

text = text.replace(
complex_block_pattern,
(_match: string, $1: string) => {
return `{ #${$1}}\n\n`;
},
);

text = text.replace(block_pattern, (match: string, $1: string) => {
return `\n{ #${$1}}\n`;
});

return text;
return replaceBlockIDs(text);
};

removeObsidianComments: TCompilerStep = () => (text) => {
Expand Down
65 changes: 65 additions & 0 deletions src/compiler/createBlockIDs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { replaceBlockIDs } from "./replaceBlockIDs";

describe("replaceBlockIDs", () => {
it("should replace block IDs in markdown", () => {
const EXPECTED_MARKDOWN = `
# header

foo ^block-id-1234

bar ^block-id-5678
`;

const result = replaceBlockIDs(EXPECTED_MARKDOWN);

expect(result).toBe(`
# header

foo
{ #block-id-1234}


bar
{ #block-id-5678}

`);
});

it("should not replace block IDs in code blocks", () => {
const CODEBLOCK_WITH_BLOCKIDS = `
\`\`\`
foobar.
this is a code block.
but it contains a block ID to try to fool the compiler
and, consequently, wreck your garden.
here it goes: ^block-id-1234
and for fun, here's another: ^block-id-5678
\`\`\`

additionally, block IDs outside of code blocks should be replaced
for example, ^block-id-9999
and ^block-id-0000
`;

const result = replaceBlockIDs(CODEBLOCK_WITH_BLOCKIDS);

expect(result).toBe(`
\`\`\`
foobar.
this is a code block.
but it contains a block ID to try to fool the compiler
and, consequently, wreck your garden.
here it goes: ^block-id-1234
and for fun, here's another: ^block-id-5678
\`\`\`

additionally, block IDs outside of code blocks should be replaced
for example,
{ #block-id-9999}

and
{ #block-id-0000}

`);
});
});
35 changes: 35 additions & 0 deletions src/compiler/replaceBlockIDs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export function replaceBlockIDs(markdown: string) {
const block_pattern = / \^([\w\d-]+)/g;
const complex_block_pattern = /\n\^([\w\d-]+)\n/g;

// To ensure code blocks are not modified...
const codeBlockPattern = /```[\s\S]*?```/g;

// Extract code blocks and replace them with placeholders
const codeBlocks: string[] = [];

markdown = markdown.replace(codeBlockPattern, (match) => {
codeBlocks.push(match);

return `{{CODE_BLOCK_${codeBlocks.length - 1}}}`;
});

// Replace patterns outside code blocks
markdown = markdown.replace(
complex_block_pattern,
(_match: string, $1: string) => {
return `{ #${$1}}\n\n`;
},
);

markdown = markdown.replace(block_pattern, (_match: string, $1: string) => {
return `\n{ #${$1}}\n`;
});

// Reinsert code blocks
codeBlocks.forEach((block, index) => {
markdown = markdown.replace(`{{CODE_BLOCK_${index}}}`, block);
});

return markdown;
}
26 changes: 26 additions & 0 deletions src/dg-testVault/015 Code blocks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
dg-publish: true
---
These codeblocks should not be modified upon publish.

Sample 1
```jinja2
{{ highlight_text }}{% if highlight_location and highlight_location_url %} ([via]({{highlight_location_url}})){% elif highlight_location %} ({{highlight_location}}){% endif %} ^rwhi{{highlight_id}}
{% if highlight_note %}
{{ highlight_note }} ^rwhi{{highlight_id}}-note
{% endif %}
```

Sample 2
```md
In medieval Latin a florilegium (plural florilegia) was a compilation of excerpts from other writings.
The word is from the Latin flos (flower) and legere (to gather): literally a gathering of flowers, or collection of fine extracts from the body of a larger work. ([via](https://en.wikipedia.org/wiki/Florilegium)) ^rwhi724352030
```

Sample 3
```
This codeblock has a transclusion syntax in it.
Check it out: ![[001 Links]]
```

And for sanity, here's some block references outside of code blocks: foobar ^test-123
51 changes: 50 additions & 1 deletion src/test/snapshot/snapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,55 @@ this is just text i guess
---


==========
015 Code blocks.md
==========
---
{"dg-publish":true,"permalink":"/015-code-blocks/"}
---

These codeblocks should not be modified upon publish.

Sample 1
```jinja2
{{ highlight_text }}{% if highlight_location and highlight_location_url %} ([via]({{highlight_location_url}})){% elif highlight_location %} ({{highlight_location}}){% endif %} ^rwhi{{highlight_id}}
{% if highlight_note %}
{{ highlight_note }} ^rwhi{{highlight_id}}-note
{% endif %}
```

Sample 2
```md
In medieval Latin a florilegium (plural florilegia) was a compilation of excerpts from other writings.
The word is from the Latin flos (flower) and legere (to gather): literally a gathering of flowers, or collection of fine extracts from the body of a larger work. ([via](https://en.wikipedia.org/wiki/Florilegium)) ^rwhi724352030
```

Sample 3
```
This codeblock has a transclusion syntax in it.
Check it out:
<div class="transclusion internal-embed is-loaded"><a class="markdown-embed-link" href="/001-links/" aria-label="Open link"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="svg-icon lucide-link"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a><div class="markdown-embed">
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noting that this is actually evidence of a very similar bug: #599





[[002 Hidden page]]

[[003 Non published page]]

[[000 Home| Aliased link to home]]

[[000 Home | Link containing whitespace which works in obsidian but doesn't in garden :) - yes, this could be a ticket but lo and behold]]



</div></div>

```

And for sanity, here's some block references outside of code blocks: foobar
{ #test-123}

==========
E Embeds/E02 PNG published.md
==========
Expand Down Expand Up @@ -715,7 +764,7 @@ P Plugins/PD Dataview/PD3 Inline JS queries.md


3
106
108
<p><span>A paragraph</span></p>

/img/user/A Assets/travolta.png
Expand Down
Loading