Skip to content

Commit

Permalink
Handle the find-replace in the script myself
Browse files Browse the repository at this point in the history
  • Loading branch information
timothymcmackin committed Jul 25, 2024
1 parent f6050b5 commit 664de29
Showing 1 changed file with 16 additions and 43 deletions.
59 changes: 16 additions & 43 deletions src/scripts/process_downloaded_glossary.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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 <div class='imported-glossary'></div> to apply custom styles
const wrapper = new JSDOM('<div id="imported-glossary"></div>');
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 ');
Expand Down

0 comments on commit 664de29

Please sign in to comment.