From cad880fe306b68b5d04e312c0105c0d15e29f20b Mon Sep 17 00:00:00 2001 From: Jacob Date: Mon, 10 Nov 2025 19:54:45 +0100 Subject: [PATCH 01/11] 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 23bc540be9..363bfc2d17 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 d12b1fb0dc..3fa3cd799b 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 95055e9fa8..ba4714eeb5 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 c7e9ca4aa5..937184d95f 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 02/11] 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 175e95d2de..cd42bbc69b 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 03/11] 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 ba4714eeb5..0d589ebcc9 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 04/11] 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 0d589ebcc9..aa8b7a35fc 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 05/11] 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 aa8b7a35fc..dca2717c00 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 06/11] 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 dca2717c00..d8e745e22b 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 07/11] 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 937184d95f..ff306209e9 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 08/11] 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 cd42bbc69b..13f9d8a754 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: From 0e78f600e9513a58f7709b80cd10f4c47f6f580c Mon Sep 17 00:00:00 2001 From: Jacob Date: Fri, 21 Nov 2025 22:34:38 +0100 Subject: [PATCH 09/11] Rename fragment identifier line omit variables in code --- src/core/render/compiler.js | 2 +- src/core/render/embed.js | 4 ++-- test/integration/example.test.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/core/render/compiler.js b/src/core/render/compiler.js index 363bfc2d17..d9a8a063e2 100644 --- a/src/core/render/compiler.js +++ b/src/core/render/compiler.js @@ -135,7 +135,7 @@ export class Compiler { } embed.fragment = config.fragment; - embed.fragmentFullLine = config.fragmentFullLine; + embed.omitFragmentLine = config.omitFragmentLine; return embed; } diff --git a/src/core/render/embed.js b/src/core/render/embed.js index d8e745e22b..f8351b22d9 100644 --- a/src/core/render/embed.js +++ b/src/core/render/embed.js @@ -77,7 +77,7 @@ function walkFetchEmbed({ embedTokens, compile, fetch }, cb) { text = extractFragmentContent( text, currentToken.embed.fragment, - currentToken.embed.fragmentFullLine, + currentToken.embed.omitFragmentLine, ); } @@ -87,7 +87,7 @@ function walkFetchEmbed({ embedTokens, compile, fetch }, cb) { text = extractFragmentContent( text, currentToken.embed.fragment, - currentToken.embed.fragmentFullLine, + currentToken.embed.omitFragmentLine, ); } diff --git a/test/integration/example.test.js b/test/integration/example.test.js index ff306209e9..2530f97e27 100644 --- a/test/integration/example.test.js +++ b/test/integration/example.test.js @@ -179,7 +179,7 @@ describe('Creating a Docsify site (integration tests in Jest)', function () { homepage: ` # Embed Test - [filename](_media/example1.html ':include :type=code :fragment=demo :fragmentFullLine') + [filename](_media/example1.html ':include :type=code :fragment=demo :omitFragmentLine') `, }, routes: { From 08523dd8c57c1081e11a9b1e12032b1bd813ee6f Mon Sep 17 00:00:00 2001 From: Jake Date: Fri, 21 Nov 2025 22:36:00 +0100 Subject: [PATCH 10/11] Update docs/embed-files.md Co-authored-by: Koy Zhuang --- 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 13f9d8a754..aba1bfaaf5 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 identifier to be omitted in the fragment output you can add the option `:fragmentFullLine`. +Alternatively you can use `### [demo]`. By default, only identifiers are omitted. To omit the entire line containing the identifier in the fragment output, you can add the `:omitFragmentLine` option. Example: From 96078a6574844f62d570d7be3b31977c73818016 Mon Sep 17 00:00:00 2001 From: Jacob Date: Fri, 21 Nov 2025 22:41:26 +0100 Subject: [PATCH 11/11] Clarify comment full line fragment identifier regex option --- 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 f8351b22d9..5312132462 100644 --- a/src/core/render/embed.js +++ b/src/core/render/embed.js @@ -22,7 +22,7 @@ function extractFragmentContent(text, fragment, fullLine) { let fragmentRegex = `(?:###|\\/\\/\\/)\\s*\\[${fragment}\\]`; const contentRegex = `[\\s\\S]*?`; if (fullLine) { - // Match full line for fragment + // Match full line containing fragment identifier (e.g. /// [demo]) fragmentRegex = `.*${fragmentRegex}.*\n`; } const pattern = new RegExp(