From a092a2af75594957164a207e2c29a6111e7749c2 Mon Sep 17 00:00:00 2001 From: "Mr.Hope" Date: Sun, 20 Aug 2023 12:09:03 +0800 Subject: [PATCH] fix(markdown): handle data uri correctly, close #1393 --- .../src/plugins/assetsPlugin/resolveLink.ts | 7 +- .../tests/plugins/assetsPlugin.spec.ts | 70 ++++++++++++------- 2 files changed, 50 insertions(+), 27 deletions(-) diff --git a/packages/markdown/src/plugins/assetsPlugin/resolveLink.ts b/packages/markdown/src/plugins/assetsPlugin/resolveLink.ts index e42da2a9ca..bec7c91c13 100644 --- a/packages/markdown/src/plugins/assetsPlugin/resolveLink.ts +++ b/packages/markdown/src/plugins/assetsPlugin/resolveLink.ts @@ -6,8 +6,11 @@ export const resolveLink = ( link: string, relativePathPrefix: string, env: MarkdownEnv, - strict = false, + strict = false ): string => { + // do not resolve data uri + if (link.startsWith('data:')) return link + // decode link to ensure bundler can find the file correctly let resolvedLink = decode(link) @@ -23,7 +26,7 @@ export const resolveLink = ( if (isRelativePath && env.filePathRelative) { resolvedLink = `${relativePathPrefix}/${path.join( path.dirname(env.filePathRelative), - resolvedLink, + resolvedLink )}` } diff --git a/packages/markdown/tests/plugins/assetsPlugin.spec.ts b/packages/markdown/tests/plugins/assetsPlugin.spec.ts index 7626b5ef92..7935a3b073 100644 --- a/packages/markdown/tests/plugins/assetsPlugin.spec.ts +++ b/packages/markdown/tests/plugins/assetsPlugin.spec.ts @@ -34,6 +34,8 @@ describe('@vuepress/markdown > plugins > assetsPlugin', () => { '![invalid](.../invalid.png)', '![汉字](.../汉字.png)', '![100%](.../100%.png)', + // data uri + '![data-uri](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+P+/HgAFhAJ/wr4H/wAAAABJRU5ErkJggg==)', ] const testCases: { @@ -77,6 +79,8 @@ describe('@vuepress/markdown > plugins > assetsPlugin', () => { 'invalid', '汉字', '100%', + // data uri + 'data-uri', ], }, { @@ -116,6 +120,8 @@ describe('@vuepress/markdown > plugins > assetsPlugin', () => { 'invalid', '汉字', '100%', + // data uri + 'data-uri', ], }, { @@ -152,6 +158,8 @@ describe('@vuepress/markdown > plugins > assetsPlugin', () => { 'invalid', '汉字', '100%', + // data uri + 'data-uri', ], }, ] @@ -159,9 +167,9 @@ describe('@vuepress/markdown > plugins > assetsPlugin', () => { testCases.forEach(({ description, md, env, expected }) => it(description, () => { expect(md.render(source.join('\n\n'), env)).toEqual( - expected.map((item) => `

${item}

`).join('\n') + '\n', + expected.map((item) => `

${item}

`).join('\n') + '\n' ) - }), + }) ) }) @@ -236,6 +244,9 @@ describe('@vuepress/markdown > plugins > assetsPlugin', () => { 'attrs', // keep as is 'attrs', + + /* data uri */ + '', ] const testCases: { @@ -319,6 +330,9 @@ describe('@vuepress/markdown > plugins > assetsPlugin', () => { 'attrs', // keep as is 'attrs', + + /* data uri */ + '', ], }, { @@ -398,6 +412,9 @@ describe('@vuepress/markdown > plugins > assetsPlugin', () => { 'attrs', // keep as is 'attrs', + + /* data uri */ + '', ], }, { @@ -474,6 +491,9 @@ describe('@vuepress/markdown > plugins > assetsPlugin', () => { 'attrs', // keep as is 'attrs', + + /* data uri */ + '', ], }, ] @@ -482,43 +502,43 @@ describe('@vuepress/markdown > plugins > assetsPlugin', () => { it(description, () => { // block expect(md.render(source.join('\n\n'), env)).toEqual( - expected.map((item) => `${item}`).join('\n'), + expected.map((item) => `${item}`).join('\n') ) // block with leading white space expect( - md.render(source.map((item) => ` ${item}`).join('\n\n'), env), + md.render(source.map((item) => ` ${item}`).join('\n\n'), env) ).toEqual(expected.map((item) => ` ${item}`).join('\n')) // inline with prefix expect( - md.render(source.map((item) => `foo${item}`).join('\n\n'), env), + md.render(source.map((item) => `foo${item}`).join('\n\n'), env) ).toEqual( - expected.map((item) => `

foo${item}

`).join('\n') + '\n', + expected.map((item) => `

foo${item}

`).join('\n') + '\n' ) // inline with suffix expect( - md.render(source.map((item) => `${item}foo`).join('\n\n'), env), + md.render(source.map((item) => `${item}foo`).join('\n\n'), env) ).toEqual( - expected.map((item) => `

${item}foo

`).join('\n') + '\n', + expected.map((item) => `

${item}foo

`).join('\n') + '\n' ) // inline with line break expect( md.render( source.map((item) => item.replace(' `

${item.replace('`) - .join('\n') + '\n', + .join('\n') + '\n' ) // wrapped item expect( - md.render(source.map((item) => `

${item}

`).join('\n\n'), env), + md.render(source.map((item) => `

${item}

`).join('\n\n'), env) ).toEqual(expected.map((item) => `

${item}

`).join('\n')) // wrapped item with line break @@ -527,20 +547,20 @@ describe('@vuepress/markdown > plugins > assetsPlugin', () => { source .map((item) => `

${item.replace('`) .join('\n\n'), - env, - ), + env + ) ).toEqual( expected .map((item) => `

${item.replace('`) - .join('\n'), + .join('\n') ) // wrapped multiple items expect( md.render( source.map((item) => `

${item}${item}

`).join('\n\n'), - env, - ), + env + ) ).toEqual(expected.map((item) => `

${item}${item}

`).join('\n')) // deeply wrapped multiple items @@ -549,14 +569,14 @@ describe('@vuepress/markdown > plugins > assetsPlugin', () => { source .map((item) => `

\n\n${item}\n${item}\n\n

`) .join('\n\n'), - env, - ), + env + ) ).toEqual( expected .map((item) => `

\n\n${item}\n${item}\n\n

`) - .join('\n'), + .join('\n') ) - }), + }) ) }) @@ -655,16 +675,16 @@ describe('@vuepress/markdown > plugins > assetsPlugin', () => { it(description, () => { // double quote expect(md.render(source.join('\n\n'), env)).toEqual( - expected.map((item) => `${item}`).join('\n') + '\n', + expected.map((item) => `${item}`).join('\n') + '\n' ) // single quote expect( - md.render(source.join('\n\n').replace(/"/g, "'"), env), + md.render(source.join('\n\n').replace(/"/g, "'"), env) ).toEqual( expected.map((item) => `${item}`.replace(/"/g, "'")).join('\n') + - '\n', + '\n' ) - }), + }) ) }) })