Further Updates to MD
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
#!/usr/bin/env node
|
||||
import { readdir, readFile, writeFile } from 'node:fs/promises'
|
||||
import path from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import process from 'node:process'
|
||||
import { marked } from 'marked'
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url)
|
||||
const __dirname = path.dirname(__filename)
|
||||
const repoRoot = path.resolve(__dirname, '..')
|
||||
|
||||
const args = new Set(process.argv.slice(2))
|
||||
const shouldWrite = args.has('--write')
|
||||
|
||||
const skipDirs = new Set(['.git', 'node_modules'])
|
||||
|
||||
const fixers = [
|
||||
// `**token**` -> **`token`**
|
||||
[/(?<!`)`\*\*((?:(?!\*\*)[^`\n])+)\*\*`/g, '**`$1`**'],
|
||||
// `**token`** -> **`token`**
|
||||
[/(?<!`)`\*\*((?:(?!`\*\*)[^`\n])+?)`\*\*/g, '**`$1`**'],
|
||||
// **`token**` -> **`token`**
|
||||
[/\*\*`((?:(?!\*\*`)[^`\n])+)\*\*`/g, '**`$1`**'],
|
||||
// **`[link](path)`** -> **[link](path)**
|
||||
[/\*\*`\[([^\]\n]+)\]\(([^)\n]+)\)`\*\*/g, '**[$1]($2)**'],
|
||||
// `**[link](path)**` -> **[link](path)**
|
||||
[/`\*\*\[([^\]\n]+)\]\(([^)\n]+)\)\*\*`/g, '**[$1]($2)**'],
|
||||
// `**[ctx` API changelog](path)** -> **[ctx API changelog](path)**
|
||||
[/`\*\*\[([^\]`\n]+)`([^\]\n]*)\]\(([^)\n]+)\)\*\*`/g, '**[$1$2]($3)**'],
|
||||
// **`[ctx API changelog](path)`** -> **[ctx API changelog](path)**
|
||||
[/\*\*`\[([^\]\n]+)\]\(([^)\n]+)\)`\*\*/g, '**[$1]($2)**']
|
||||
]
|
||||
|
||||
const suspiciousPatterns = [
|
||||
/(?<!`)`\*\*((?:(?!\*\*)[^`\n])+)\*\*`/,
|
||||
/(?<!`)`\*\*((?:(?!`\*\*)[^`\n])+?)`\*\*/,
|
||||
/\*\*`((?:(?!\*\*`)[^`\n])+)\*\*`/
|
||||
]
|
||||
|
||||
async function collectMarkdownFiles(dir) {
|
||||
const entries = await readdir(dir, { withFileTypes: true })
|
||||
const files = []
|
||||
for (const entry of entries) {
|
||||
const full = path.join(dir, entry.name)
|
||||
if (entry.isDirectory()) {
|
||||
if (skipDirs.has(entry.name)) continue
|
||||
files.push(...(await collectMarkdownFiles(full)))
|
||||
continue
|
||||
}
|
||||
if (entry.isFile() && entry.name.endsWith('.md')) files.push(full)
|
||||
}
|
||||
return files
|
||||
}
|
||||
|
||||
function renderedTextContainsDoubleAsterisk(markdown) {
|
||||
const html = marked.parse(markdown)
|
||||
const text = String(html).replace(/<[^>]+>/g, ' ')
|
||||
return text.includes('**')
|
||||
}
|
||||
|
||||
function applyFixers(markdown) {
|
||||
let out = markdown
|
||||
for (const [pattern, replacement] of fixers) {
|
||||
out = out.replace(pattern, replacement)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
function hasSuspiciousSourcePattern(markdown) {
|
||||
return suspiciousPatterns.some((pattern) => pattern.test(markdown))
|
||||
}
|
||||
|
||||
const files = await collectMarkdownFiles(repoRoot)
|
||||
const changed = []
|
||||
const flagged = []
|
||||
|
||||
for (const filePath of files) {
|
||||
const original = await readFile(filePath, 'utf8')
|
||||
const fixed = applyFixers(original)
|
||||
const hasRenderedDoubleAsterisk = renderedTextContainsDoubleAsterisk(fixed)
|
||||
const hasSuspiciousPatternRemaining = hasSuspiciousSourcePattern(fixed)
|
||||
|
||||
if (shouldWrite && fixed !== original) {
|
||||
await writeFile(filePath, fixed, 'utf8')
|
||||
changed.push(path.relative(repoRoot, filePath))
|
||||
}
|
||||
|
||||
if (hasRenderedDoubleAsterisk && hasSuspiciousPatternRemaining) {
|
||||
flagged.push(path.relative(repoRoot, filePath))
|
||||
}
|
||||
}
|
||||
|
||||
if (changed.length) {
|
||||
console.log(`Updated ${changed.length} markdown files:`)
|
||||
for (const file of changed) console.log(`- ${file}`)
|
||||
}
|
||||
|
||||
if (flagged.length) {
|
||||
console.log('\nRendered output plus suspicious source pattern still found in:')
|
||||
for (const file of flagged) console.log(`- ${file}`)
|
||||
console.log('\nInspect these manually or extend fixers in scripts/fix-markdown-rendered-asterisks.mjs.')
|
||||
process.exitCode = 1
|
||||
} else {
|
||||
console.log('No rendered literal "**" remains after checks.')
|
||||
}
|
||||
Reference in New Issue
Block a user