Files
noodl-docs/plugins/copy-node-markdowns.js
Eric Tuvesson 53f0d6320e Initial commit
Co-Authored-By: kotte <14197736+mrtamagotchi@users.noreply.github.com>
Co-Authored-By: mikaeltellhed <2311083+mikaeltellhed@users.noreply.github.com>
Co-Authored-By: Tore Knudsen <18231882+torekndsn@users.noreply.github.com>
Co-Authored-By: Michael Cartner <32543275+michaelcartner@users.noreply.github.com>
2023-09-05 12:08:55 +02:00

58 lines
1.8 KiB
JavaScript

const fs = require('fs')
const path = require('path')
function resolveImports(content,dir) {
const includeMatch = content.matchAll(/@include\s\"(.*)\"/g)
for(const _s of includeMatch) {
const includePath = _s[1];
const absPath = path.join(dir,includePath)
const include = fs.readFileSync(absPath)
content = content.replace(_s[0],include)
}
return content
}
function copyNodeMarkdowns(dir) {
fs.readdirSync(dir).forEach(file => {
if( fs.lstatSync(dir + '/' + file).isDirectory() ) {
copyNodeMarkdowns(dir + '/' + file)
}
else if(file.endsWith('.md')) {
const content = fs.readFileSync(dir + '/' + file)
const resolved = resolveImports(content.toString(),dir)
const filePath = 'build/' + dir + '/' + file;
if (!fs.existsSync('build/' + dir)){
fs.mkdirSync('build/' + dir);
}
fs.writeFileSync(filePath, resolved)
}
})
}
module.exports = function(context, options) {
return {
name: 'docusaurus-copy-node-markdowns-plugin',
configureWebpack(config, isServer, utils) {
const {getJSLoader} = utils;
return isServer?{
plugins: [
{
apply: (compiler) => {
compiler.hooks.afterEmit.tap('AfterEmitPlugin', (compilation) => {
console.log('Copying node markdown files')
options.paths.forEach(path => {
console.log(' - Processing path: ' + path)
copyNodeMarkdowns(path)
})
})
}
}
]
}:{}
},
};
};