Something went wrong. Try again.
handy typescript function to turn a man page into markdown
Something went wrong. Try again.
778 B · 32 lines · Created 1w ago
TypeScript
123456789101112131415161718192021222324252627282930313233// CC0-1.0, made by @hotsocket.fyi and @vera.rechnerin.deimport { execFileSync } from "node:child_process";import type { SpawnSyncReturns } from "node:child_process";
// gets a man page and tries to return a unix status code and its contents// rendered to markdown.export default function( page: string, section: number|undefined): [number,string] { try { const manPath = execFileSync( 'man', [ '-w', ...(section?['-S',`${section}`]:[]), '--', page ], {encoding: 'utf-8'} ).split('\n',1)[0]; const rendered = execFileSync( 'mandoc', ['-T','markdown',manPath], {encoding: 'utf-8'} ); return [0, rendered]; } catch (_e){ const e = _e as SpawnSyncReturns<string>; return [e.status ?? 1, e.stderr.split('\n',1)[0]]; }}