Skip to content

Commit 620ae80

Browse files
committed
modify script plugin so that snippets update on build permanently
1 parent 4388250 commit 620ae80

File tree

5 files changed

+81
-58
lines changed

5 files changed

+81
-58
lines changed

docusaurus.config.en.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import chHeader from "./plugins/header.js";
55
import fixLinks from "./src/hooks/fixLinks.js";
66
const path = require('path');
77
const remarkCustomBlocks = require('./plugins/remark-custom-blocks');
8-
const remarkCodeImport = require('./plugins/remark-code-import');
8+
const codeImportPlugin = require('./plugins/code-import-plugin');
99

1010
// Import custom plugins
1111
const { customParseFrontMatter } = require('./plugins/frontmatter-validation/customParseFrontMatter');
@@ -153,7 +153,7 @@ const config = {
153153
showLastUpdateTime: false,
154154
sidebarCollapsed: true,
155155
routeBasePath: "/",
156-
remarkPlugins: [math, remarkCustomBlocks, glossaryTransformer, [remarkCodeImport, { baseDir: __dirname }]],
156+
remarkPlugins: [math, remarkCustomBlocks, glossaryTransformer],
157157
beforeDefaultRemarkPlugins: [fixLinks],
158158
rehypePlugins: [katex],
159159
},
@@ -356,6 +356,10 @@ const config = {
356356
[
357357
'./plugins/tailwind-config.js',
358358
{}
359+
],
360+
[
361+
codeImportPlugin,
362+
{}
359363
]
360364
],
361365
customFields: {

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
"node-fetch": "^3.3.2",
6464
"numeral": "^2.0.6",
6565
"prism-react-renderer": "^2.4.1",
66-
"raw-loader": "^4.0.2",
6766
"react": "^18.2.0",
6867
"react-dom": "^18.2.0",
6968
"react-medium-image-zoom": "^5.2.14",

plugins/code-import-plugin.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const glob = require('glob');
4+
5+
function codeImportPlugin(context, options) {
6+
return {
7+
name: 'code-import-plugin',
8+
async loadContent() {
9+
// Find all markdown files in docs directory that might contain code imports
10+
const docsPath = path.join(context.siteDir, 'docs');
11+
12+
const markdownFiles = [
13+
...glob.sync('**/*.md', { cwd: docsPath, absolute: true }),
14+
...glob.sync('**/*.mdx', { cwd: docsPath, absolute: true }),
15+
];
16+
17+
// Process each markdown file for code imports
18+
const processedFiles = [];
19+
20+
for (const filePath of markdownFiles) {
21+
try {
22+
let content = fs.readFileSync(filePath, 'utf8');
23+
let modified = false;
24+
25+
// Process code blocks with file= syntax
26+
content = content.replace(/```(\w+)?\s*(file=[^\s\n]+)([^\n]*)\n([^`]*?)```/g, (match, lang, fileParam, additionalMeta, existingContent) => {
27+
try {
28+
const importPath = fileParam.replace('file=', '');
29+
const absoluteImportPath = path.resolve(context.siteDir, importPath);
30+
const importedContent = fs.readFileSync(absoluteImportPath, 'utf8');
31+
modified = true;
32+
33+
// Preserve the complete metadata including file= and any additional parameters
34+
const fullMeta = `${fileParam}${additionalMeta}`;
35+
const metaStr = fullMeta ? ` ${fullMeta}` : '';
36+
37+
return `\`\`\`${lang || ''}${metaStr}\n${importedContent}\`\`\``;
38+
} catch (error) {
39+
console.warn(`Could not import file ${importPath} in ${filePath}: ${error.message}`);
40+
return match; // Return original if import fails
41+
}
42+
});
43+
44+
if (modified) {
45+
processedFiles.push({
46+
path: filePath,
47+
content: content,
48+
originalPath: filePath
49+
});
50+
}
51+
} catch (error) {
52+
console.warn(`Error processing file ${filePath}: ${error.message}`);
53+
}
54+
}
55+
56+
return { processedFiles };
57+
},
58+
59+
async contentLoaded({ content, actions }) {
60+
const { processedFiles } = content;
61+
62+
// Write processed files back to disk during build
63+
for (const file of processedFiles) {
64+
try {
65+
fs.writeFileSync(file.path, file.content, 'utf8');
66+
console.log(`Processed code imports in: ${path.relative(context.siteDir, file.path)}`);
67+
} catch (error) {
68+
console.error(`Error writing processed file ${file.path}: ${error.message}`);
69+
}
70+
}
71+
}
72+
};
73+
}
74+
75+
module.exports = codeImportPlugin;

plugins/remark-code-import.js

Lines changed: 0 additions & 47 deletions
This file was deleted.

yarn.lock

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11754,14 +11754,6 @@ raw-body@2.5.2:
1175411754
iconv-lite "0.4.24"
1175511755
unpipe "1.0.0"
1175611756

11757-
raw-loader@^4.0.2:
11758-
version "4.0.2"
11759-
resolved "https://registry.yarnpkg.com/raw-loader/-/raw-loader-4.0.2.tgz#1aac6b7d1ad1501e66efdac1522c73e59a584eb6"
11760-
integrity sha512-ZnScIV3ag9A4wPX/ZayxL/jZH+euYb6FcUinPcgiQW0+UBtEv0O6Q3lGd3cqJ+GHH+rksEv3Pj99oxJ3u3VIKA==
11761-
dependencies:
11762-
loader-utils "^2.0.0"
11763-
schema-utils "^3.0.0"
11764-
1176511757
rc@1.2.8, rc@^1.2.7:
1176611758
version "1.2.8"
1176711759
resolved "https://registry.yarnpkg.com/rc/-/rc-1.2.8.tgz#cd924bf5200a075b83c188cd6b9e211b7fc0d3ed"

0 commit comments

Comments
 (0)