3import { existsSync, readdirSync } from 'fs' 9export const bashtranscriptsDir = path.join(ptDir, 'pt0/peatsite/bashtranscripts') 11export const getBashExamples = (epKey) => { 12 if (!existsSync(bashtranscriptsDir)) return [] 13 const prefix = `${epKey}-` 14 return readdirSync(bashtranscriptsDir) 15 .filter(f => f.startsWith(prefix) && f.endsWith('.html')) 16 .map(f => f.slice(prefix.length, -5)) 19export const linkifyEpPath = (htmlHelp, plainHelp) => { 20 const epPathMatch = plainHelp.match(new RegExp(`${ptnodeBin}\\s+(pt0\\/[^\\s]+)`)) 24 htmlHelp = htmlHelp.replace(escapedPath, `<a href="${srcUrl}">${escapedPath}</a>`) 26 if (plainHelp.match(new RegExp(`^\\$ ${ptmBin} `, 'm'))) { 28 htmlHelp = htmlHelp.replace(`$ ${ptmBin} `, `$ <a href="${srcUrl}">${ptmBin}</a> `) 33export const linkifyActions = (htmlHelp, plainHelp, epName, exampleActions) => { 34 const lines = plainHelp.split('\n') 35 for (const line of lines) { 36 const actionMatch = line.match(/^ (\w+)(\s{2,}|\s*$)/) 37 if (!actionMatch) continue 38 const [, actionName, spacing] = actionMatch 39 if (!exampleActions.includes(actionName)) continue 40 const plainNeedle = ` ${actionName}${spacing}` 42 const linkedAction = ` <a class="action-link" data-action="${actionName}" data-ep="${epName}">${escapeHtml(actionName)}</a>${escapeHtml(spacing)}` 43 htmlHelp = htmlHelp.replace(escapedNeedle, linkedAction) 45 const flagVariants = exampleActions.filter(a => a.includes('--')) 46 for (const exAction of flagVariants) { 47 const dashIdx = exAction.indexOf('--') 48 const baseAction = exAction.slice(0, dashIdx) 49 const flag = exAction.slice(dashIdx) 52 const lineIdx = htmlHelp.indexOf(lineNeedle) 53 if (lineIdx === -1) continue 54 const lineEnd = htmlHelp.indexOf('\n', lineIdx) 55 const before = htmlHelp.slice(0, lineIdx) 56 const lineSlice = htmlHelp.slice(lineIdx, lineEnd === -1 ? undefined : lineEnd) 57 const after = lineEnd === -1 ? '' : htmlHelp.slice(lineEnd) 58 if (!lineSlice.includes(flagText)) continue 59 const flagLink = `<a class="action-link" data-action="${exAction}" data-ep="${epName}">${flagText}</a>` 60 htmlHelp = before + lineSlice.replace(flagText, flagLink) + after 65export const linkifyEpHelpLinks = (htmlHelp, epPathToSlug) => { 66 for (const [epPath, slug] of epPathToSlug) { 68 if (!htmlHelp.includes(escapedPath)) continue 69 if (htmlHelp.includes(`>${escapedPath}</a>`)) continue 70 htmlHelp = htmlHelp.replace(escapedPath, `<a href="/blogposts/${slug}/index.html">${escapedPath}</a>`) 75export const helpPageScript = ` 77document.querySelectorAll('.action-link').forEach(link => { 78 link.addEventListener('click', (e) => { 80 const action = e.target.dataset.action, ep = e.target.dataset.ep 81 const tpl = document.getElementById('bash-' + ep + '-' + action) 82 const out = document.getElementById('action-output') 83 if (!tpl || !out) return 84 document.querySelectorAll('.action-link').forEach(l => l.classList.remove('active')) 85 e.target.classList.add('active') 86 out.innerHTML = '<div class="bash-output">' + tpl.innerHTML + '</div>'