Skip to content

Commit 4ecc34e

Browse files
Your Namesy-records
authored andcommitted
fix(embed): scope cached includes to their page context
1 parent 91c923a commit 4ecc34e

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

‎src/core/render/embed.js‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,12 @@ function walkFetchEmbed({ embedTokens, compile, fetch, frontMatter }, cb) {
132132
}
133133

134134
export function prerenderEmbed({ compiler, raw = '', fetch }, done) {
135-
const hit = cached[raw];
135+
const cacheKey = JSON.stringify([
136+
compiler.contentBase,
137+
compiler.router.getCurrentPath(),
138+
raw,
139+
]);
140+
const hit = cached[cacheKey];
136141
if (hit) {
137142
const copy = hit.slice();
138143
copy.links = hit.links;
@@ -271,8 +276,8 @@ export function prerenderEmbed({ compiler, raw = '', fetch }, done) {
271276
}
272277
}
273278
} else if (!token) {
274-
cached[raw] = tokens.concat();
275-
tokens.links = cached[raw].links = links;
279+
cached[cacheKey] = tokens.concat();
280+
tokens.links = cached[cacheKey].links = links;
276281
done(tokens);
277282
}
278283
},

‎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+
});

0 commit comments

Comments
 (0)