Skip to content

Commit 66530e4

Browse files
committed
Added render tests for rootRelativeImageURL config
1 parent 6335294 commit 66530e4

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

test/_helper.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ module.exports.init = function(
5454

5555
const rootPath = path.join(__dirname, 'fixtures', fixture);
5656

57-
const dom = initJSDOM(markup);
57+
const runScriptInJSDom =
58+
Object.values(config).length !== 0 ? 'dangerously' : undefined;
59+
const dom = initJSDOM(markup, { runScripts: runScriptInJSDom });
5860
dom.reconfigure({ url: 'file:///' + rootPath });
5961

6062
// Mimic src/core/index.js but for Node.js

test/unit/render.test.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const path = require('path');
12
const { expect } = require('chai');
23
const { init, expectSameDom } = require('../_helper');
34

@@ -104,6 +105,23 @@ describe('render', function() {
104105
);
105106
});
106107

108+
it('relative image url', async function() {
109+
const { docsify } = await init();
110+
const output = docsify.compiler.compile(
111+
'![alt text](some-path/image.png)'
112+
);
113+
const expectedCompiledPath = path.posix.join(
114+
// must use posix here because if run on windows, paths are joined using backslash (\)
115+
__dirname,
116+
'../fixtures/default',
117+
'some-path/image.png'
118+
);
119+
expectSameDom(
120+
output,
121+
`<p><img src="${expectedCompiledPath}" data-origin="some-path/image.png" alt="alt text"></p>`
122+
);
123+
});
124+
107125
it('class', async function() {
108126
const { docsify } = await init();
109127
const output = docsify.compiler.compile(
@@ -165,6 +183,53 @@ describe('render', function() {
165183
);
166184
});
167185
});
186+
187+
describe('compiling root-relative image src path', async function() {
188+
it('behaves normally if config.rootRelativeImageURL is set to false', async function() {
189+
const { docsify } = await init('default', {
190+
rootRelativeImageURL: false,
191+
});
192+
const rootRelativePathOutput = docsify.compiler.compile(
193+
'![alt text](/some-path/image.png)'
194+
);
195+
const expectedCompiledPath = path.posix.join(
196+
// must use posix here because if run on windows, file paths use backslash (\)
197+
__dirname,
198+
'../fixtures/default',
199+
'some-path/image.png'
200+
);
201+
expectSameDom(
202+
rootRelativePathOutput,
203+
`<p><img src="${expectedCompiledPath}" data-origin="/some-path/image.png" alt="alt text"></p>`
204+
);
205+
});
206+
207+
it('only uses the image href if config.rootRelativeImageURL is set to true', async function() {
208+
const { docsify } = await init('default', {
209+
rootRelativeImageURL: true,
210+
});
211+
const rootRelativePathOutput = docsify.compiler.compile(
212+
'![alt text](/some-path/image.png)'
213+
);
214+
expectSameDom(
215+
rootRelativePathOutput,
216+
`<p><img src="/some-path/image.png" data-origin="/some-path/image.png" alt="alt text"></p>`
217+
);
218+
});
219+
220+
it('uses config.rootRelativeImageURL as a prefix for the compiled image path, if passed as a string', async function() {
221+
const { docsify } = await init('default', {
222+
rootRelativeImageURL: 'docs',
223+
});
224+
const rootRelativePathOutput = docsify.compiler.compile(
225+
'![alt text](/some-path/image.png)'
226+
);
227+
expectSameDom(
228+
rootRelativePathOutput,
229+
`<p><img src="/docs/some-path/image.png" data-origin="/some-path/image.png" alt="alt text"></p>`
230+
);
231+
});
232+
});
168233
});
169234

170235
describe('heading', function() {

0 commit comments

Comments
 (0)