46 lines
1.3 KiB
JavaScript
46 lines
1.3 KiB
JavaScript
![]() |
import fs from 'fs';
|
||
|
import Showdown from 'showdown';
|
||
|
import 'dotenv/config';
|
||
|
import { ENV } from './lib/env.js';
|
||
|
|
||
|
const converter = new Showdown.Converter();
|
||
|
|
||
|
let inputDir = 'content/';
|
||
|
if(ENV.getIsSet('VS_INPUT_DIR')) {
|
||
|
inputDir = ENV.getString('VS_INPUT_DIR');
|
||
|
}
|
||
|
|
||
|
let outputDir = 'build/';
|
||
|
if(ENV.getIsSet('VS_OUTPUT_DIR')) {
|
||
|
outputDir = ENV.getString('VS_OUTPUT_DIR');
|
||
|
}
|
||
|
|
||
|
const fileEntities = fs.readdirSync(inputDir, {recursive: true, withFileTypes: true, encoding: 'utf8'});
|
||
|
|
||
|
fileEntities.forEach((fileEnt) => {
|
||
|
const filenameTokens = fileEnt.name.split('.');
|
||
|
const extension = filenameTokens[filenameTokens.length - 1];
|
||
|
|
||
|
if(extension === 'md') {
|
||
|
const fullPath = fileEnt.parentPath + fileEnt.name;
|
||
|
|
||
|
const content = fs.readFileSync(fullPath, {encoding: 'utf8'});
|
||
|
|
||
|
const converted = converter.makeHtml(content);
|
||
|
|
||
|
let outputFileName = "";
|
||
|
for(let i = 0; i < filenameTokens.length - 1; i++) {
|
||
|
outputFileName += filenameTokens[i];
|
||
|
}
|
||
|
|
||
|
outputFileName += '.html';
|
||
|
|
||
|
const trimmedPath = fileEnt.parentPath.replace(inputDir, "");
|
||
|
|
||
|
const outputPath = outputDir + trimmedPath + outputFileName;
|
||
|
|
||
|
fs.writeFileSync(outputPath, converted);
|
||
|
|
||
|
console.log(`Converted ${fullPath} -> ${outputPath}`);
|
||
|
}
|
||
|
});
|