Skip to content

Commit f1bf3b6

Browse files
authored
Merge branch 'develop' into dependabot/npm_and_yarn/postcss-import-17.0.1
2 parents f4f59bb + 0e0cd50 commit f1bf3b6

3 files changed

Lines changed: 85 additions & 5 deletions

File tree

‎src/core/render/embed.js‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { stripIndent } from 'common-tags';
2+
import { getParentPath, getPath } from '../router/util.js';
23
import { get } from '../util/ajax.js';
34

4-
const cached = {};
5+
const cached = new Map();
56

67
/**
78
* Extracts the content between matching fragment markers in the text.
@@ -132,9 +133,20 @@ function walkFetchEmbed({ embedTokens, compile, fetch, frontMatter }, cb) {
132133
}
133134

134135
export function prerenderEmbed({ compiler, raw = '', fetch }, done) {
135-
const hit = cached[raw];
136+
// Match the base used by compileEmbed to resolve relative includes.
137+
const contentDirectory = getPath(
138+
compiler.contentBase,
139+
getParentPath(compiler.router.getCurrentPath()),
140+
);
141+
let directoryCache = cached.get(contentDirectory);
142+
if (!directoryCache) {
143+
directoryCache = new Map();
144+
cached.set(contentDirectory, directoryCache);
145+
}
146+
147+
const hit = directoryCache.get(raw);
136148
if (hit) {
137-
const copy = hit.slice();
149+
const copy = hit.tokens.slice();
138150
copy.links = hit.links;
139151
return done(copy);
140152
}
@@ -271,8 +283,8 @@ export function prerenderEmbed({ compiler, raw = '', fetch }, done) {
271283
}
272284
}
273285
} else if (!token) {
274-
cached[raw] = tokens.concat();
275-
tokens.links = cached[raw].links = links;
286+
directoryCache.set(raw, { tokens: tokens.concat(), links });
287+
tokens.links = links;
276288
done(tokens);
277289
}
278290
},

‎test/e2e/embed.test.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import docsifyInit from '../helpers/docsify-init.js';
2+
import { test, expect } from './fixtures/docsify-init-fixture.js';
3+
4+
test('resolves identical includes relative to each page directory', async ({
5+
page,
6+
}) => {
7+
const markdown = '# Example\n\n[snippet](snippet.js ":include :type=code")';
8+
9+
await docsifyInit({
10+
markdown: {
11+
homepage: '# Home',
12+
sidebar: '- [First](first/)\n- [Second](second/)',
13+
},
14+
routes: {
15+
'/first/README.md': markdown,
16+
'/second/README.md': markdown,
17+
'/first/snippet.js': 'const example = "first";',
18+
'/second/snippet.js': 'const example = "second";',
19+
},
20+
});
21+
22+
const sidebar = page.locator('.sidebar-nav');
23+
const code = page.locator('#main pre code');
24+
25+
await sidebar.getByRole('link', { name: 'First', exact: true }).click();
26+
await expect(code).toHaveText('const example = "first";');
27+
await sidebar.getByRole('link', { name: 'Second', exact: true }).click();
28+
await expect(code).toHaveText('const example = "second";');
29+
await sidebar.getByRole('link', { name: 'First', exact: true }).click();
30+
await expect(code).toHaveText('const example = "first";');
31+
});

‎test/unit/embed-cache.test.js‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { prerenderEmbed } from '../../src/core/render/embed.js';
2+
3+
test('reuses prerendered tokens across anchors without mixing content directories', () => {
4+
let currentPath = '/first/?id=one';
5+
let lexerCalls = 0;
6+
const compiler = {
7+
contentBase: '/docs/',
8+
router: { getCurrentPath: () => currentPath },
9+
config: {},
10+
_marked: {
11+
lexer: () => {
12+
lexerCalls += 1;
13+
return Object.assign([], { links: {} });
14+
},
15+
},
16+
};
17+
const render = target =>
18+
prerenderEmbed({ compiler: target, raw: '# Example' }, () => {});
19+
20+
render(compiler);
21+
currentPath = '/first/?id=two';
22+
render(compiler);
23+
currentPath = '/first/page';
24+
render(compiler);
25+
expect(lexerCalls).toBe(1);
26+
27+
currentPath = '/second/';
28+
render(compiler);
29+
expect(lexerCalls).toBe(2);
30+
31+
currentPath = '/first/';
32+
render(compiler);
33+
expect(lexerCalls).toBe(2);
34+
35+
render({ ...compiler, contentBase: '/other/' });
36+
expect(lexerCalls).toBe(3);
37+
});

0 commit comments

Comments
 (0)