| 1 |
/** |
| 2 |
* Remark plugin to render YAML files as markdown tables. |
| 3 |
* |
| 4 |
* Usage in markdown: |
| 5 |
* ::yaml-table{file="git-command-table.yml"} |
| 6 |
* |
| 7 |
* The YAML file should be an array of objects. Each object becomes a row, |
| 8 |
* and the keys become column headers. |
| 9 |
*/ |
| 10 |
|
| 11 |
import fs from 'node:fs'; |
| 12 |
import path from 'node:path'; |
| 13 |
import yaml from 'js-yaml'; |
| 14 |
import { visit } from 'unist-util-visit'; |
| 15 |
import { fromMarkdown } from 'mdast-util-from-markdown'; |
| 16 |
import { gfm } from 'micromark-extension-gfm'; |
| 17 |
import { gfmFromMarkdown } from 'mdast-util-gfm'; |
| 18 |
|
| 19 |
export default function remarkYamlTable(options = {}) { |
| 20 |
const { basePath = process.cwd() } = options; |
| 21 |
|
| 22 |
return (tree, file) => { |
| 23 |
const replacements = []; |
| 24 |
|
| 25 |
// Find all ::yaml-table directives |
| 26 |
visit(tree, 'leafDirective', (node, index, parent) => { |
| 27 |
if (node.name !== 'yaml-table') return; |
| 28 |
|
| 29 |
const filePath = node.attributes?.file; |
| 30 |
if (!filePath) { |
| 31 |
console.warn('::yaml-table directive missing "file" attribute'); |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
// Resolve the file path relative to the docs directory |
| 36 |
const resolvedPath = path.resolve(basePath, filePath); |
| 37 |
|
| 38 |
if (!fs.existsSync(resolvedPath)) { |
| 39 |
console.warn(`YAML file not found: ${resolvedPath}`); |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
const content = fs.readFileSync(resolvedPath, 'utf-8'); |
| 44 |
let data; |
| 45 |
try { |
| 46 |
data = yaml.load(content); |
| 47 |
} catch (e) { |
| 48 |
console.warn(`Failed to parse YAML file: ${resolvedPath}`, e); |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
if (!Array.isArray(data) || data.length === 0) { |
| 53 |
console.warn(`YAML file should contain an array: ${resolvedPath}`); |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
// Get column headers from the first object's keys |
| 58 |
const headers = Object.keys(data[0]); |
| 59 |
|
| 60 |
// Build markdown table |
| 61 |
const headerRow = '| ' + headers.join(' | ') + ' |'; |
| 62 |
const separatorRow = '| ' + headers.map(() => '---').join(' | ') + ' |'; |
| 63 |
|
| 64 |
const dataRows = data.map((row) => { |
| 65 |
const cells = headers.map((header) => { |
| 66 |
let value = row[header] ?? ''; |
| 67 |
// Convert to string and escape pipe characters |
| 68 |
value = String(value).trim().replace(/\|/g, '\\|'); |
| 69 |
// Replace newlines with spaces for table cells |
| 70 |
value = value.replace(/\n/g, ' '); |
| 71 |
return value; |
| 72 |
}); |
| 73 |
return '| ' + cells.join(' | ') + ' |'; |
| 74 |
}); |
| 75 |
|
| 76 |
const tableMarkdown = [headerRow, separatorRow, ...dataRows].join('\n'); |
| 77 |
|
| 78 |
// Parse the table markdown into AST nodes |
| 79 |
const tableTree = fromMarkdown(tableMarkdown, { |
| 80 |
extensions: [gfm()], |
| 81 |
mdastExtensions: [gfmFromMarkdown()], |
| 82 |
}); |
| 83 |
|
| 84 |
// Store for later replacement |
| 85 |
replacements.push({ index, parent, nodes: tableTree.children }); |
| 86 |
}); |
| 87 |
|
| 88 |
// Replace directives with table content (in reverse to maintain indices) |
| 89 |
for (const { index, parent, nodes } of replacements.reverse()) { |
| 90 |
parent.children.splice(index, 1, ...nodes); |
| 91 |
} |
| 92 |
}; |
| 93 |
} |