From cad880fe306b68b5d04e312c0105c0d15e29f20b Mon Sep 17 00:00:00 2001 From: Jacob Date: Mon, 10 Nov 2025 19:54:45 +0100 Subject: [PATCH 1/8] Add full line ignore to embed fragment --- src/core/render/compiler.js | 1 + src/core/render/compiler/media.js | 4 +-- src/core/render/embed.js | 26 ++++++++++++++----- test/integration/example.test.js | 42 +++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 8 deletions(-) diff --git a/src/core/render/compiler.js b/src/core/render/compiler.js index 23bc540be..363bfc2d1 100644 --- a/src/core/render/compiler.js +++ b/src/core/render/compiler.js @@ -135,6 +135,7 @@ export class Compiler { } embed.fragment = config.fragment; + embed.fragmentFullLine = config.fragmentFullLine; return embed; } diff --git a/src/core/render/compiler/media.js b/src/core/render/compiler/media.js index d12b1fb0d..3fa3cd799 100644 --- a/src/core/render/compiler/media.js +++ b/src/core/render/compiler/media.js @@ -18,12 +18,12 @@ export const compileMedia = { }, video(url, title) { return { - html: ``, + html: ``, }; }, audio(url, title) { return { - html: ``, + html: ``, }; }, code(url, title) { diff --git a/src/core/render/embed.js b/src/core/render/embed.js index 95055e9fa..ba4714eeb 100644 --- a/src/core/render/embed.js +++ b/src/core/render/embed.js @@ -12,16 +12,22 @@ const cached = {}; * * @param {string} text - The input text that may contain embedded fragments. * @param {string} fragment - The fragment identifier to search for. + * @param {boolean} fullLine - The fragment identifier to search for. * @returns {string} - The extracted and demented content, or an empty string if not found. */ -function extractFragmentContent(text, fragment) { +function extractFragmentContent(text, fragment, fullLine) { if (!fragment) { return text; } - + let fragmentRegex = `###|\\/\\/\\/)\\s*\\[${fragment}\\]`; + const contentRegex = `[\\s\\S]*?`; + if (fullLine) { + // Match full line for fragment + fragmentRegex = `.*${fragmentRegex}.*\n`; + } const pattern = new RegExp( - `(?:###|\\/\\/\\/)\\s*\\[${fragment}\\]([\\s\\S]*?)(?:###|\\/\\/\\/)\\s*\\[${fragment}\\]`, - ); + `(?:${fragmentRegex})(${contentRegex})(?:${fragmentRegex})`, + ); // content is the capture group const match = text.match(pattern); return stripIndent((match || [])[1] || '').trim(); } @@ -68,13 +74,21 @@ function walkFetchEmbed({ embedTokens, compile, fetch }, cb) { } if (currentToken.embed.fragment) { - text = extractFragmentContent(text, currentToken.embed.fragment); + text = extractFragmentContent( + text, + currentToken.embed.fragment, + currentToken.embed.fragmentFullLine, + ); } embedToken = compile.lexer(text); } else if (currentToken.embed.type === 'code') { if (currentToken.embed.fragment) { - text = extractFragmentContent(text, currentToken.embed.fragment); + text = extractFragmentContent( + text, + currentToken.embed.fragment, + currentToken.embed.fragmentFullLine, + ); } embedToken = compile.lexer( diff --git a/test/integration/example.test.js b/test/integration/example.test.js index c7e9ca4aa..937184d95 100644 --- a/test/integration/example.test.js +++ b/test/integration/example.test.js @@ -173,6 +173,48 @@ describe('Creating a Docsify site (integration tests in Jest)', function () { ); }); + test('embed file full line fragment identifier', async () => { + await docsifyInit({ + markdown: { + homepage: ` + # Embed Test + + [filename](_media/example1.html ':include :type=code :fragment=demo :fragmentFullLine') + `, + }, + routes: { + '_media/example1.html': ` + + `, + }, + }); + + // Wait for the embedded fragment to be fetched and rendered into #main + expect( + await waitForText('#main', 'console.log(JSON.stringify(myJson));'), + ).toBeTruthy(); + + const mainText = document.querySelector('#main').textContent; + expect(mainText).not.toContain('https://api.example.com/data'); + expect(mainText).not.toContain('Full line fragment identifier'); + expect(mainText).not.toContain('-->'); + expect(mainText).not.toContain( + 'result.then(console.log).catch(console.error);', + ); + }); + test('embed multiple file code fragments', async () => { await docsifyInit({ markdown: { From bf6eadb082daf62f794616c6b31601749e6130d3 Mon Sep 17 00:00:00 2001 From: Jacob Date: Mon, 10 Nov 2025 20:00:17 +0100 Subject: [PATCH 2/8] Add docs --- docs/embed-files.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/embed-files.md b/docs/embed-files.md index 175e95d2d..cd42bbc69 100644 --- a/docs/embed-files.md +++ b/docs/embed-files.md @@ -65,7 +65,7 @@ Sometimes you don't want to embed a whole file. Maybe because you need just a fe ``` In your code file you need to surround the fragment between `/// [demo]` lines (before and after the fragment). -Alternatively you can use `### [demo]`. +Alternatively you can use `### [demo]`. If you want the full line containing the fragment identifier you can add the option `:fragmentFullLine`. Example: From 7754abf653136f239f0d2caf27c2e34d02046410 Mon Sep 17 00:00:00 2001 From: Jacob Date: Mon, 10 Nov 2025 20:55:23 +0100 Subject: [PATCH 3/8] Fix regex --- src/core/render/embed.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/render/embed.js b/src/core/render/embed.js index ba4714eeb..0d589ebcc 100644 --- a/src/core/render/embed.js +++ b/src/core/render/embed.js @@ -19,7 +19,7 @@ function extractFragmentContent(text, fragment, fullLine) { if (!fragment) { return text; } - let fragmentRegex = `###|\\/\\/\\/)\\s*\\[${fragment}\\]`; + let fragmentRegex = `###|\\/\\/\\/\\s*\\[${fragment}\\]`; const contentRegex = `[\\s\\S]*?`; if (fullLine) { // Match full line for fragment From 86231b0a178bcaa76bf1cd9d8b7d5745d0f51bcd Mon Sep 17 00:00:00 2001 From: Jake Date: Tue, 11 Nov 2025 10:53:59 +0100 Subject: [PATCH 4/8] Update src/core/render/embed.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/core/render/embed.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/render/embed.js b/src/core/render/embed.js index 0d589ebcc..aa8b7a35f 100644 --- a/src/core/render/embed.js +++ b/src/core/render/embed.js @@ -12,7 +12,7 @@ const cached = {}; * * @param {string} text - The input text that may contain embedded fragments. * @param {string} fragment - The fragment identifier to search for. - * @param {boolean} fullLine - The fragment identifier to search for. + * @param {boolean} fullLine - Boolean flag to enable full-line matching of fragment identifiers. * @returns {string} - The extracted and demented content, or an empty string if not found. */ function extractFragmentContent(text, fragment, fullLine) { From de7eae2bb2883a2e0d5d3199822b18b0d86bd458 Mon Sep 17 00:00:00 2001 From: Jake Date: Tue, 11 Nov 2025 13:56:22 +0100 Subject: [PATCH 5/8] Update src/core/render/embed.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/core/render/embed.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/render/embed.js b/src/core/render/embed.js index aa8b7a35f..dca2717c0 100644 --- a/src/core/render/embed.js +++ b/src/core/render/embed.js @@ -19,7 +19,7 @@ function extractFragmentContent(text, fragment, fullLine) { if (!fragment) { return text; } - let fragmentRegex = `###|\\/\\/\\/\\s*\\[${fragment}\\]`; + let fragmentRegex = `(?:###|\\/\\/\\/)\\s*\\[${fragment}\\]`; const contentRegex = `[\\s\\S]*?`; if (fullLine) { // Match full line for fragment From ea027bc8c1ab18f305c2e1e66dcfce6f2d788e3f Mon Sep 17 00:00:00 2001 From: Jake Date: Tue, 11 Nov 2025 13:57:23 +0100 Subject: [PATCH 6/8] Update src/core/render/embed.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/core/render/embed.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/render/embed.js b/src/core/render/embed.js index dca2717c0..d8e745e22 100644 --- a/src/core/render/embed.js +++ b/src/core/render/embed.js @@ -13,7 +13,7 @@ const cached = {}; * @param {string} text - The input text that may contain embedded fragments. * @param {string} fragment - The fragment identifier to search for. * @param {boolean} fullLine - Boolean flag to enable full-line matching of fragment identifiers. - * @returns {string} - The extracted and demented content, or an empty string if not found. + * @returns {string} - The extracted and dedented content, or an empty string if not found. */ function extractFragmentContent(text, fragment, fullLine) { if (!fragment) { From 39fc2de44fea3edab3ca7ccae786dbd2b6c9ba21 Mon Sep 17 00:00:00 2001 From: Jacob Date: Tue, 11 Nov 2025 21:15:47 +0100 Subject: [PATCH 7/8] Add test to verify missing fragment yields no output --- test/integration/example.test.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/test/integration/example.test.js b/test/integration/example.test.js index 937184d95..ff306209e 100644 --- a/test/integration/example.test.js +++ b/test/integration/example.test.js @@ -228,7 +228,9 @@ describe('Creating a Docsify site (integration tests in Jest)', function () { # Text between [filename](_media/example3.js ':include :fragment=something_else_not_code') - + + [filename](_media/example4.js ':include :fragment=demo') + # Text after `, }, @@ -251,6 +253,12 @@ describe('Creating a Docsify site (integration tests in Jest)', function () { example3 += 10; /// [something_else_not_code] console.log(example3);`, + '_media/example4.js': ` + let example4 = 1; + ### No fragment here + example4 += 10; + /// No fragment here + console.log(example4);`, }, }); @@ -267,5 +275,8 @@ describe('Creating a Docsify site (integration tests in Jest)', function () { expect(mainText).not.toContain('console.log(example1);'); expect(mainText).not.toContain('console.log(example2);'); expect(mainText).not.toContain('console.log(example3);'); + expect(mainText).not.toContain('console.log(example4);'); + expect(mainText).not.toContain('example4 += 10;'); + expect(mainText).not.toContain('No fragment here'); }); }); From 77c090308aa84f5d64a0b02ec680002f7b931f42 Mon Sep 17 00:00:00 2001 From: Jacob Date: Thu, 13 Nov 2025 08:46:37 +0100 Subject: [PATCH 8/8] Fix docs description --- docs/embed-files.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/embed-files.md b/docs/embed-files.md index cd42bbc69..13f9d8a75 100644 --- a/docs/embed-files.md +++ b/docs/embed-files.md @@ -65,7 +65,7 @@ Sometimes you don't want to embed a whole file. Maybe because you need just a fe ``` In your code file you need to surround the fragment between `/// [demo]` lines (before and after the fragment). -Alternatively you can use `### [demo]`. If you want the full line containing the fragment identifier you can add the option `:fragmentFullLine`. +Alternatively you can use `### [demo]`. If you want the full line containing the identifier to be omitted in the fragment output you can add the option `:fragmentFullLine`. Example: