From 664de29cc42e5f4e170e226fbc2b213cc1fa694f Mon Sep 17 00:00:00 2001 From: Tim McMackin Date: Thu, 25 Jul 2024 16:39:56 -0400 Subject: [PATCH] Handle the find-replace in the script myself --- src/scripts/process_downloaded_glossary.js | 59 ++++++---------------- 1 file changed, 16 insertions(+), 43 deletions(-) diff --git a/src/scripts/process_downloaded_glossary.js b/src/scripts/process_downloaded_glossary.js index d66f660a6..fd1f73b81 100644 --- a/src/scripts/process_downloaded_glossary.js +++ b/src/scripts/process_downloaded_glossary.js @@ -25,59 +25,21 @@ const download = async (url, filename) => { }); } -// Run child process as promise -const runProcess = async (scriptFileName, targetFileName, cwd, outputFileName) => { - if (fs.existsSync(outputFileName)) { - await fs.promises.unlink(outputFileName); - } - return new Promise((resolve, reject) => { - const relativePathToScript = path.relative(path.dirname(targetFileName), scriptFileName); - const run = spawn(relativePathToScript + ' ' + path.basename(targetFileName) + '> ' + outputFileName, [], { - cwd, - // Use shell: true here so the script can use the `> output_file` syntax to write output to a file - shell: true, - }); - - run.stderr.on('data', (data) => { - reject(data); - }); - - run.on('close', (code) => { - resolve(); - }); - }); -} - const process_glossary = async () => { const repoRoot = path.resolve(__dirname, '../../'); - // Conversion script expects the downloaded glossary file to be in _build/ - const buildFolder = path.resolve(repoRoot, '_glossaryBuild/_build'); + const buildFolder = path.resolve(repoRoot, '_glossaryBuild/'); if (!fs.existsSync(buildFolder)){ fs.mkdirSync(buildFolder, { recursive: true }); } - // Script file in _scripts/ - const scriptFolder = path.resolve(repoRoot, '_glossaryBuild/_scripts'); - if (!fs.existsSync(scriptFolder)){ - fs.mkdirSync(scriptFolder, { recursive: true }); - } // Download glossary and conversion script const glossarySourceFileName = path.resolve(buildFolder, 'glossary.html'); - const scriptFileName = path.resolve(scriptFolder, 'extract_content'); const glossaryFilePromise = download('https://tezos.gitlab.io/active/glossary.html', glossarySourceFileName); - const scriptFilePromise = download('https://gitlab.com/tezos/tezos/-/raw/master/docs/scripts/extract_content?ref_type=heads&inline=false', scriptFileName); - await Promise.all([glossaryFilePromise, scriptFilePromise]); - await fs.promises.chmod(scriptFileName, '777'); - - // Run conversion script - const outputFileName = path.resolve(buildFolder, 'extracted_content.html'); - await runProcess(scriptFileName, glossarySourceFileName, buildFolder, outputFileName); - console.log('Used Octez docs script to pull the content from its glossary'); - const conversionScriptOutputFile = await fs.promises.readFile(outputFileName, 'utf8'); + await Promise.resolve(glossaryFilePromise); - const downloadedGlossaryDom = new JSDOM(conversionScriptOutputFile); + const downloadedGlossaryDom = new JSDOM(await fs.promises.readFile(glossarySourceFileName, 'utf8')); // Trim html header, body, and such out const trimmed = downloadedGlossaryDom.window.document.querySelector('#glossary'); @@ -92,13 +54,24 @@ const process_glossary = async () => { link.setAttribute('target', '_blank'); }); + // Internal links go to Octez docs + const internalLinks = trimmed.querySelectorAll('a.reference.internal'); + internalLinks.forEach((link) => { + if (link.getAttribute('href').startsWith('../')) { + const newHref = link.getAttribute('href').replace('../', 'https://tezos.gitlab.io/'); + link.setAttribute('href', newHref); + link.setAttribute('target', '_blank'); + } else if (!link.getAttribute('href').startsWith('#')) { + link.setAttribute('href', 'https://tezos.gitlab.io/active/' + link.getAttribute('href')); + link.setAttribute('target', '_blank'); + } + }); + // Wrap with
to apply custom styles const wrapper = new JSDOM('
'); const imported_glossary = wrapper.window.document.querySelector('div#imported-glossary'); imported_glossary.appendChild(trimmed); - - // Convert to string and remove line breaks to prevent MDX processing from making them into paragraph tags let imported_glossary_str = imported_glossary.outerHTML; imported_glossary_str = imported_glossary_str.replace(/([^>])$\n/gm, '$1 ');