Skip to content

Commit 4dabea0

Browse files
finetjuldaker
andcommitted
docs(examples): add button to show example code
And open example in full screen Co-authored-by: Adnane Belmadiaf <daker@users.noreply.github.com>
1 parent 378b6d8 commit 4dabea0

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

Documentation/scripts/generate-examples.mjs

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ async function walkExamples(config, dir = config.root, results = []) {
5050
const fullPath = path.join(dir, entry.name);
5151
if (entry.isDirectory()) {
5252
const relDir = path.relative(config.root, fullPath).replace(/\\/g, '/');
53-
// console.log('Visiting directory:', relDir);
5453
if (relDir && config.shouldSkipDir(relDir)) {
5554
return;
5655
}
@@ -62,11 +61,13 @@ async function walkExamples(config, dir = config.root, results = []) {
6261
if (!config.isExampleFile(relPath)) return;
6362
const exampleUrl = buildExampleUrl(config.baseSegment, relPath);
6463
const exampleName = config.getExampleName(fullPath, relPath);
64+
const exampleCode = await fs.readFile(fullPath, 'utf-8');
6565
// Link convention
6666
const link = `./examples/${exampleName}.html`;
6767
results.push({
6868
title: exampleName,
6969
link,
70+
exampleCode,
7071
exampleUrl,
7172
});
7273
}
@@ -75,8 +76,9 @@ async function walkExamples(config, dir = config.root, results = []) {
7576
return results;
7677
}
7778

78-
function buildExampleMarkdown(name) {
79+
function buildExampleMarkdown(name, exampleCode) {
7980
const examplePage = `${name}/index.html`;
81+
const encodedExampleCode = encodeURIComponent(exampleCode);
8082
return `---
8183
layout: page
8284
sidebar: false
@@ -85,14 +87,53 @@ title: ${name}
8587
8688
<!-- This file is auto-generated by scripts/generate-examples.mjs -->
8789
<ClientOnly>
88-
<div style="height: calc(100vh - 154px);">
90+
<div style="position: relative; height: calc(100vh - 154px);">
8991
<iframe
9092
src="${examplePage}"
9193
title="${name} example"
9294
loading="lazy"
93-
style="width: 100%; height: 100%; border: none;"
95+
style="position: absolute; width: 100%; height: 100%; border: none;"
9496
allowfullscreen
9597
></iframe>
98+
<div style="position: absolute; bottom: 20px; left: 20px; z-index: 40; display: flex; flex-direction: column; gap: 8px;" >
99+
<a href="${examplePage}" target="_blank">
100+
<button style="
101+
padding: 8px 12px; background-color: rgba(50,50,50,0.6); color: white;
102+
border: none; cursor: pointer; border-radius: 4px;
103+
">Full screen</button>
104+
</a>
105+
<button
106+
id="showSourceBtn"
107+
data-source="${encodedExampleCode}"
108+
style="
109+
padding: 8px 12px; background-color: rgba(50,50,50,0.6); color: white;
110+
border: none; cursor: pointer; border-radius: 4px;
111+
"
112+
onclick="
113+
if (this.dataset.state === 'off') {
114+
this.textContent = 'Hide JS';
115+
this.dataset.state = 'on';
116+
document.getElementById('sourceContent').textContent = decodeURIComponent(this.dataset.source);
117+
document.getElementById('sourceModal').style.display = 'block';
118+
} else {
119+
this.textContent = 'Show JS';
120+
this.dataset.state = 'off';
121+
document.getElementById('sourceModal').style.display = 'none';
122+
}
123+
"
124+
data-state="off"
125+
>Show JS</button>
126+
</div>
127+
<div
128+
id="sourceModal"
129+
style="
130+
display: none; position: absolute; width: 100%; height: 100%;
131+
background: rgba(245,245,245,0.8); z-index: 20; color: black;
132+
overflow: auto; padding-top: 20px; padding-left: 130px; font-family: monospace;
133+
"
134+
>
135+
<pre id="sourceContent"></pre>
136+
</div>
96137
</div>
97138
</ClientOnly>
98139
`;
@@ -139,8 +180,8 @@ async function writeMarkdownFiles(examples) {
139180
await removeStaleExampleDirs(validNames);
140181

141182
await Promise.all(
142-
examples.map(async ({ title, exampleUrl }) => {
143-
const mdContent = buildExampleMarkdown(title);
183+
examples.map(async ({ title, exampleCode }) => {
184+
const mdContent = buildExampleMarkdown(title, exampleCode);
144185
const mdPath = path.join(OUTPUT_FOLDER, `${title}.md`);
145186
await fs.writeFile(mdPath, mdContent, 'utf-8');
146187
})

0 commit comments

Comments
 (0)