From 34a93b8cdaac54398d4a58c13ce734daec3fc91e Mon Sep 17 00:00:00 2001 From: Tyler Nieman Date: Wed, 29 May 2024 18:01:24 -0700 Subject: [PATCH 1/4] add problematic code blocks --- src/dg-testVault/015 Code blocks.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/dg-testVault/015 Code blocks.md diff --git a/src/dg-testVault/015 Code blocks.md b/src/dg-testVault/015 Code blocks.md new file mode 100644 index 00000000..1a6601d0 --- /dev/null +++ b/src/dg-testVault/015 Code blocks.md @@ -0,0 +1,23 @@ +--- +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 +``` + +And for sanity, here's some block references outside of code blocks: +foobar ^test-123 +and another below +^test-456 \ No newline at end of file From f2b7deb9df3acbea85ff6b7623dc5bde0e012dc9 Mon Sep 17 00:00:00 2001 From: Tyler Nieman Date: Wed, 29 May 2024 20:06:11 -0700 Subject: [PATCH 2/4] dont replace block-ids in codeblocks --- src/compiler/GardenPageCompiler.ts | 17 +------ src/compiler/createBlockIDs.test.ts | 69 +++++++++++++++++++++++++++++ src/compiler/replaceBlockIDs.ts | 35 +++++++++++++++ 3 files changed, 106 insertions(+), 15 deletions(-) create mode 100644 src/compiler/createBlockIDs.test.ts create mode 100644 src/compiler/replaceBlockIDs.ts diff --git a/src/compiler/GardenPageCompiler.ts b/src/compiler/GardenPageCompiler.ts index 1dc54acf..ec08fe4d 100644 --- a/src/compiler/GardenPageCompiler.ts +++ b/src/compiler/GardenPageCompiler.ts @@ -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; @@ -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) => { diff --git a/src/compiler/createBlockIDs.test.ts b/src/compiler/createBlockIDs.test.ts new file mode 100644 index 00000000..9d4e1960 --- /dev/null +++ b/src/compiler/createBlockIDs.test.ts @@ -0,0 +1,69 @@ +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 + + below + ^block-id-9999 + `; + + const result = replaceBlockIDs(EXPECTED_MARKDOWN); + + expect(result).toBe(` + # header + + foo + { #block-id-1234} + + bar + { #block-id-5678} + + below + { #block-id-9999} + `); + }); + + 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} + + `); + }); +}); diff --git a/src/compiler/replaceBlockIDs.ts b/src/compiler/replaceBlockIDs.ts new file mode 100644 index 00000000..50eb1a65 --- /dev/null +++ b/src/compiler/replaceBlockIDs.ts @@ -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; +} From 92fda9195dc2eea75a01cf0c1a6308e3aaa9080c Mon Sep 17 00:00:00 2001 From: Tyler Nieman Date: Mon, 10 Jun 2024 20:54:17 -0700 Subject: [PATCH 3/4] adjust createBlockIDs test + sample data --- src/compiler/createBlockIDs.test.ts | 10 +++------- src/dg-testVault/015 Code blocks.md | 13 ++++++++----- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/compiler/createBlockIDs.test.ts b/src/compiler/createBlockIDs.test.ts index 9d4e1960..b05ab68b 100644 --- a/src/compiler/createBlockIDs.test.ts +++ b/src/compiler/createBlockIDs.test.ts @@ -8,9 +8,6 @@ describe("replaceBlockIDs", () => { foo ^block-id-1234 bar ^block-id-5678 - - below - ^block-id-9999 `; const result = replaceBlockIDs(EXPECTED_MARKDOWN); @@ -19,13 +16,12 @@ describe("replaceBlockIDs", () => { # header foo - { #block-id-1234} +{ #block-id-1234} + bar - { #block-id-5678} +{ #block-id-5678} - below - { #block-id-9999} `); }); diff --git a/src/dg-testVault/015 Code blocks.md b/src/dg-testVault/015 Code blocks.md index 1a6601d0..d6fe2b69 100644 --- a/src/dg-testVault/015 Code blocks.md +++ b/src/dg-testVault/015 Code blocks.md @@ -7,7 +7,7 @@ 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 +{{ highlight_note }} ^rwhi{{highlight_id}}-note {% endif %} ``` @@ -17,7 +17,10 @@ In medieval Latin a florilegium (plural florilegia) was a compilation of excerpt 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 ``` -And for sanity, here's some block references outside of code blocks: -foobar ^test-123 -and another below -^test-456 \ No newline at end of file +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 \ No newline at end of file From 3dd9cb1d28351ce22706bd14dde5eefdf3c3a2c3 Mon Sep 17 00:00:00 2001 From: Tyler Nieman Date: Mon, 10 Jun 2024 21:03:11 -0700 Subject: [PATCH 4/4] update snapshot for code block preservation of block-id syntax --- src/test/snapshot/snapshot.md | 51 ++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/src/test/snapshot/snapshot.md b/src/test/snapshot/snapshot.md index feb6017a..da1293df 100644 --- a/src/test/snapshot/snapshot.md +++ b/src/test/snapshot/snapshot.md @@ -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: +
+ + + + +[[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]] + + + +
+ +``` + +And for sanity, here's some block references outside of code blocks: foobar +{ #test-123} + ========== E Embeds/E02 PNG published.md ========== @@ -715,7 +764,7 @@ P Plugins/PD Dataview/PD3 Inline JS queries.md 3 -106 +108

A paragraph

/img/user/A Assets/travolta.png