convert.mjs 1.2 KB

123456789101112131415
  1. // Execute the pinned SPXX converter without executing any article scripts.
  2. import { build } from 'esbuild';
  3. import { JSDOM } from 'jsdom';
  4. import fs from 'node:fs/promises';
  5. import path from 'node:path';
  6. const [source, htmlFile, url, output] = process.argv.slice(2);
  7. const entry = path.resolve(source, 'packages/userscript/src/sites/minecraft-net.ts');
  8. const code = await fs.readFile(entry, 'utf8');
  9. const result = await build({stdin:{contents:code+'\nexport { convertMCArticleToMarkdown };',resolveDir:path.dirname(entry),loader:'ts'},bundle:true,write:false,platform:'node',format:'esm',plugins:[{name:'headless-config',setup(b){b.onLoad({filter:/[/\\]config\.ts$/},()=>({contents:'export default {translator:"Hilda-Bot",bugCenter:{}};',loader:'js'}));}}]});
  10. const {convertMCArticleToMarkdown} = await import('data:text/javascript;base64,'+Buffer.from(result.outputFiles[0].text).toString('base64'));
  11. const doc = new JSDOM(await fs.readFile(htmlFile,'utf8'), {url}).window.document;
  12. if (!doc.querySelector('.MC_Layout_Article')) throw new Error('Minecraft.net article layout changed');
  13. const markdown = await convertMCArticleToMarkdown(doc,url,'Hilda-Bot');
  14. if (markdown.trim().length < 200) throw new Error('Empty/truncated article');
  15. await fs.writeFile(output,markdown);