Skip to content

Commit eddbd96

Browse files
committed
fix(docs): iframe example paths
Signed-off-by: Cory Rylan <crylan@nvidia.com>
1 parent f97acb6 commit eddbd96

5 files changed

Lines changed: 132 additions & 5 deletions

File tree

‎projects/site/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@
201201
]
202202
},
203203
"test": {
204-
"command": "vitest run src/_11ty/layouts/metadata.test.ts src/_11ty/layouts/links.test.ts src/_11ty/transforms/site-urls.test.ts src/_11ty/shortcodes/api.test.ts src/_11ty/plugins/llms-txt.test.ts src/_11ty/plugins/sitemap-xml.test.ts src/docs/metrics/api-status.test.ts",
204+
"command": "vitest run src/_11ty/layouts/metadata.test.ts src/_11ty/layouts/links.test.ts src/_11ty/transforms/site-urls.test.ts src/_11ty/shortcodes/api.test.ts src/_11ty/shortcodes/example.test.ts src/_11ty/plugins/llms-txt.test.ts src/_11ty/plugins/sitemap-xml.test.ts src/docs/metrics/api-status.test.ts src/examples/index.test.ts",
205205
"files": [
206206
"src/**/*.js",
207207
"src/**/*.md",

‎projects/site/src/_11ty/shortcodes/example.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export async function exampleShortcode(
6666
consttemplateContent=example?.template.replace(/\n\n/g,'\n');
6767
constreload=globalThis.process.env.ELEVENTY_RUN_MODE==='serve' ? reloadScript(example,canvasId) : '';
6868
constinlineTemplate=/* html */`<div id="${canvasId}_content">${templateContent}</div>${reload}`;
69-
constiframeTemplate=/* html */`<iframe loading="lazy" src="examples/${example?.permalink}index.html" style="height: 100%; width: 100%; border: none;"></iframe>`;
69+
constiframeTemplate=/* html */`<iframe loading="lazy" src="/examples/${example?.permalink}index.html" style="height: 100%; width: 100%; border: none;"></iframe>`;
7070
consttemplate=config.inline ? inlineTemplate : iframeTemplate;
7171
constsummary=markdown
7272
.render(example.description||example.summary||'')
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import{afterEach,describe,expect,it,vi}from'vitest';
2+
3+
constpatternExample={
4+
id: 'pattern-chat-popover-chat',
5+
name: 'PopoverChat',
6+
template: '<nve-dialog></nve-dialog>',
7+
summary: 'Bottom-right anchored chat dialog with launcher button.',
8+
description: '',
9+
tags: ['pattern'],
10+
entrypoint: '@internals/patterns/chat.examples.json',
11+
element: 'nve-patterns',
12+
elementName: 'patterns',
13+
permalink: '@internals/patterns/chat-pattern-chat-popover-chat/'
14+
};
15+
16+
asyncfunctionimportShortcode(){
17+
vi.resetModules();
18+
vi.doMock('../../index.11tydata.js',()=>({
19+
siteData: {
20+
examples: [patternExample]
21+
}
22+
}));
23+
vi.doMock('@internals/tools/playground',()=>({
24+
PlaygroundService: {
25+
create: vi.fn().mockResolvedValue('')
26+
}
27+
}));
28+
29+
returnimport('./example.js');
30+
}
31+
32+
afterEach(()=>{
33+
vi.doUnmock('../../index.11tydata.js');
34+
vi.doUnmock('@internals/tools/playground');
35+
});
36+
37+
describe('exampleShortcode',()=>{
38+
it('should render iframe examples from the root examples route',async()=>{
39+
const{ exampleShortcode }=awaitimportShortcode();
40+
41+
consthtml=awaitexampleShortcode('@internals/patterns/chat.examples.json','PopoverChat',{
42+
inline: false
43+
});
44+
45+
expect(html).toContain('src="/examples/@internals/patterns/chat-pattern-chat-popover-chat/index.html"');
46+
expect(html).not.toContain('/docs/patterns/chat/examples/');
47+
});
48+
});

‎projects/site/src/examples/index.11ty.js‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,31 @@ function escapeAttr(value) {
2020
.replace(/'/g,'&#39;');
2121
}
2222

23-
functiongetCanonicalUrl(example){
24-
constpath=example.elementName ? `/docs/elements/${example.elementName}/examples/` : '/examples/';
23+
functiongetPatternName(example){
24+
returnexample.entrypoint?.match(/^@internals\/patterns\/([^/]+)\.examples\.json$/)?.[1]??null;
25+
}
26+
27+
exportfunctiongetCanonicalPath(example){
28+
constpatternName=getPatternName(example);
29+
30+
if(patternName)return`/docs/patterns/${patternName}/`;
31+
if(example.elementName)return`/docs/elements/${example.elementName}/examples/`;
32+
33+
return'/examples/';
34+
}
35+
36+
exportfunctiongetDocumentationPath(example){
37+
constpatternName=getPatternName(example);
38+
39+
if(patternName)return`/docs/patterns/${patternName}/`;
40+
if(example.elementName)return`/docs/elements/${example.elementName}/`;
41+
42+
return'/examples/';
43+
}
44+
45+
exportfunctiongetCanonicalUrl(example){
46+
constpath=getCanonicalPath(example);
47+
2548
return`${SITE_ORIGIN}${PATH_PREFIX}${path}`;
2649
}
2750

@@ -67,7 +90,7 @@ export async function render(data) {
6790
<body data-pagefind-ignore="all">
6891
<div id="iframe-links" nve-layout="row gap:sm align:right" hidden>
6992
<a href="${awaitPlaygroundService.create({template: data.example.template,name: data.example.id})}" target="_blank" nve-text="link body sm">playground &#8599;</a>
70-
<a href="/docs/elements/${data.example.elementName}/" target="_blank" nve-text="link body sm">documentation &#8599;</a>
93+
<a href="${getDocumentationPath(data.example)}" target="_blank" nve-text="link body sm">documentation &#8599;</a>
7194
</div>
7295
<div id="example-container" data-element="${data.example.id}">
7396
${data.example.template}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import{afterEach,describe,expect,it,vi}from'vitest';
2+
3+
interfaceExample{
4+
elementName?: string;
5+
entrypoint?: string;
6+
}
7+
8+
asyncfunctionimportExamplePage(){
9+
vi.resetModules();
10+
vi.stubEnv('ELEMENTS_SITE_URL','https://nvidia.github.io');
11+
vi.stubEnv('PAGES_BASE_URL','/elements/');
12+
vi.doMock('../index.11tydata.js',()=>({
13+
siteData: {
14+
BASE_URL: '/elements/',
15+
examples: []
16+
}
17+
}));
18+
vi.doMock('@internals/tools/playground',()=>({
19+
PlaygroundService: {
20+
create: vi.fn().mockResolvedValue('')
21+
}
22+
}));
23+
24+
returnimport('./index.11ty.js');
25+
}
26+
27+
afterEach(()=>{
28+
vi.unstubAllEnvs();
29+
vi.doUnmock('../index.11tydata.js');
30+
vi.doUnmock('@internals/tools/playground');
31+
});
32+
33+
describe('example page urls',()=>{
34+
it('should point pattern examples at pattern documentation',async()=>{
35+
const{ getCanonicalPath, getCanonicalUrl, getDocumentationPath }=awaitimportExamplePage();
36+
constexample: Example={
37+
elementName: 'patterns',
38+
entrypoint: '@internals/patterns/subheader.examples.json'
39+
};
40+
41+
expect(getCanonicalPath(example)).toBe('/docs/patterns/subheader/');
42+
expect(getDocumentationPath(example)).toBe('/docs/patterns/subheader/');
43+
expect(getCanonicalUrl(example)).toBe('https://nvidia.github.io/elements/docs/patterns/subheader/');
44+
});
45+
46+
it('should keep component examples pointed at component documentation',async()=>{
47+
const{ getCanonicalPath, getDocumentationPath }=awaitimportExamplePage();
48+
constexample: Example={
49+
elementName: 'button',
50+
entrypoint: '@nvidia-elements/core/button/button.examples.json'
51+
};
52+
53+
expect(getCanonicalPath(example)).toBe('/docs/elements/button/examples/');
54+
expect(getDocumentationPath(example)).toBe('/docs/elements/button/');
55+
});
56+
});

0 commit comments

Comments
 (0)