Skip to content

Commit d2fe558

Browse files
committed
Change script to build code samples instead of only fetching
1 parent 039ca58 commit d2fe558

3 files changed

Lines changed: 33 additions & 16 deletions

File tree

.github/workflows/post-deployment.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ jobs:
2626
- name: Install dependencies
2727
run: npm install
2828

29-
- name: Run pull-code-samples script to fetch new code samples
30-
run: node scripts/pull-code-samples.mjs
29+
- name: Build code sample snippets
30+
run: node scripts/build-code-sample-snippets.mjs
3131

3232
- name: Check for changes
3333
id: changes
@@ -39,7 +39,7 @@ jobs:
3939
if [[ $has_changes == "true" ]]; then
4040
echo "There are changes in the Git working directory."
4141
git config user.name "meili-bot"
42-
git config user.email "meili-bot@users.noreply.github.com"
42+
git config user.email "meili-bot@meilisearch.com"
4343
git add snippets/
4444
git commit -m "Update code samples"
4545
git push origin main

.github/workflows/pull-code-samples.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88
jobs:
99
update-samples:
1010
runs-on: ubuntu-latest
11-
11+
1212
steps:
1313
- name: Checkout repository
1414
uses: actions/checkout@v6
@@ -22,13 +22,13 @@ jobs:
2222
- name: Install dependencies
2323
run: npm install
2424

25-
- name: Run pull-code-samples script
26-
run: node scripts/pull-code-samples.mjs
25+
- name: Build code sample snippets
26+
run: node scripts/build-code-sample-snippets.mjs
2727

2828
- name: Commit changes
2929
run: |
3030
git config --local user.email "github-actions[bot]@users.noreply.github.com"
3131
git config --local user.name "github-actions[bot]"
3232
git add snippets/
3333
git diff --quiet && git diff --staged --quiet || git commit -m "Update code samples [skip ci]"
34-
git push
34+
git push
Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,19 @@ const REPOS = SDK.map(sdk =>
8787
);
8888

8989
const OUTPUT_DIR = path.join(process.cwd(), 'snippets/samples');
90-
if (!fs.existsSync(OUTPUT_DIR)) fs.mkdirSync(OUTPUT_DIR, { recursive: true });
9190

91+
/**
92+
* Remove all existing code sample snippets before regenerating them.
93+
* This ensures stale/unused snippets don't persist.
94+
*/
95+
function cleanSnippets() {
96+
if (!fs.existsSync(OUTPUT_DIR)) return;
9297

93-
function extractLanguageFromUrl(repoUrl) {
94-
const match = repoUrl.match(/meilisearch-([a-zA-Z]+)/);
95-
return match ? match[1] : 'text'; // Default to 'text' if not found
98+
const files = fs.readdirSync(OUTPUT_DIR).filter(f => f.startsWith('code_samples_') && f.endsWith('.mdx'));
99+
for (const file of files) {
100+
fs.unlinkSync(path.join(OUTPUT_DIR, file));
101+
}
102+
console.log(`Cleaned ${files.length} existing code sample snippets.`);
96103
}
97104

98105
async function fetchYaml(url) {
@@ -101,7 +108,14 @@ async function fetchYaml(url) {
101108
return yaml.load(await response.text());
102109
}
103110

104-
async function processRepos() {
111+
async function buildSnippets() {
112+
// Step 1: Clean existing snippets
113+
cleanSnippets();
114+
115+
// Step 2: Ensure output directory exists
116+
if (!fs.existsSync(OUTPUT_DIR)) fs.mkdirSync(OUTPUT_DIR, { recursive: true });
117+
118+
// Step 3: Fetch and aggregate all code samples
105119
const operationSnippets = {};
106120

107121
for (let i = 0; i < REPOS.length; i++) {
@@ -127,7 +141,8 @@ async function processRepos() {
127141
}
128142
}
129143

130-
// Write each sample name content to a file in the snippets folder
144+
// Step 4: Write each sample to a file in the snippets folder
145+
let count = 0;
131146
for (const [operationName, snippets] of Object.entries(operationSnippets)) {
132147
const filePath = path.join(OUTPUT_DIR, `code_samples_${operationName}.mdx`);
133148
const content = `
@@ -148,7 +163,7 @@ ${description}
148163
149164
${codeBlocks.map(block => {
150165
// Remove language identifier and code block markers to ensure Mintlify can parse it
151-
const cleanBlock = block.replace(/^[a-z]+\s*/, '').replace(/```$/, '');
166+
const cleanBlock = block.replace(/^[a-z]+\s*/, '').replace(/\`\`\`$/, '');
152167
return cleanBlock;
153168
}).join('\n')}
154169
\`\`\``;
@@ -164,8 +179,10 @@ ${snippet.content}
164179
`.trim();
165180

166181
fs.writeFileSync(filePath, content, 'utf-8');
167-
console.log(`Saved: ${operationName}.mdx`);
182+
count++;
168183
}
184+
185+
console.log(`Generated ${count} code sample snippets.`);
169186
}
170187

171-
processRepos();
188+
buildSnippets();

0 commit comments

Comments
 (0)