🌳
pt0/peatsite/binF/galleryHelpLinkifyAI.mts
1// @ts-nocheck
2import path from 'path'
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]+)`))
21 if (epPathMatch) {
22 const srcUrl = getSrcFileUrl(epPathMatch[1])
23 const escapedPath = escapeHtml(epPathMatch[1])
24 htmlHelp = htmlHelp.replace(escapedPath, `<a href="${srcUrl}">${escapedPath}</a>`)
25 }
26 if (plainHelp.match(new RegExp(`^\\$ ${ptmBin} `, 'm'))) {
27 const srcUrl = getSrcFileUrl(`pt0/path_bin/${ptmBin}`)
28 htmlHelp = htmlHelp.replace(`$ ${ptmBin} `, `$ <a href="${srcUrl}">${ptmBin}</a> `)
29 }
30 return htmlHelp
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}`
41 const escapedNeedle = escapeHtml(plainNeedle)
42 const linkedAction = ` <a class="action-link" data-action="${actionName}" data-ep="${epName}">${escapeHtml(actionName)}</a>${escapeHtml(spacing)}`
43 htmlHelp = htmlHelp.replace(escapedNeedle, linkedAction)
44 }
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)
50 const flagText = escapeHtml(`[${flag}]`)
51 const lineNeedle = escapeHtml(` ${baseAction}`)
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
61 }
62 return htmlHelp
65export const linkifyEpHelpLinks = (htmlHelp, epPathToSlug) => {
66 for (const [epPath, slug] of epPathToSlug) {
67 const escapedPath = escapeHtml(epPath)
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>`)
71 }
72 return htmlHelp
75export const helpPageScript = `
76<script>
77document.querySelectorAll('.action-link').forEach(link => {
78 link.addEventListener('click', (e) => {
79 e.preventDefault()
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>'
87 })
88})
89</script>`