|
| 1 | +import type { FormatPluginFnOptions } from "@pnpm/meta-updater"; |
| 2 | + |
| 3 | +const HEADERS = ["Character", "Default spoken form"] as const; |
| 4 | + |
| 5 | +export function updateGraphemeDefaultSpokenFormsMd( |
| 6 | + entries: Readonly<Record<string, string>>, |
| 7 | + actual: string | null, |
| 8 | + options: FormatPluginFnOptions, |
| 9 | +): string | null { |
| 10 | + if (options.manifest.name !== "@cursorless/app-web-docs") { |
| 11 | + return null; |
| 12 | + } |
| 13 | + |
| 14 | + const alphabet: string[][] = []; |
| 15 | + const digits: string[][] = []; |
| 16 | + const symbols: string[][] = []; |
| 17 | + |
| 18 | + for (const [character, spokenForm] of Object.entries(entries)) { |
| 19 | + const row = [code(character), code(spokenForm)]; |
| 20 | + |
| 21 | + if (/^[a-z]$/u.test(character)) { |
| 22 | + alphabet.push(row); |
| 23 | + } else if (/^[0-9]$/u.test(character)) { |
| 24 | + digits.push(row); |
| 25 | + } else { |
| 26 | + symbols.push(row); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + return [ |
| 31 | + "# Alphabet and symbols", |
| 32 | + "", |
| 33 | + "Cursorless uses the [Talon Community](https://github.com/talonhub/community) alphabet, digits, and symbol spoken forms via the [`user.any_alphanumeric_key`](https://github.com/talonhub/community/blob/607c3415f5f29a5f75db6fe5648e37f514f62ac5/core/keys/keys.py#L71-L74) capture.", |
| 34 | + "", |
| 35 | + "## Alphabet", |
| 36 | + "", |
| 37 | + ...formatTable(HEADERS, alphabet), |
| 38 | + "", |
| 39 | + "## Symbols", |
| 40 | + "", |
| 41 | + ...formatTable(HEADERS, symbols), |
| 42 | + "", |
| 43 | + "## Digits", |
| 44 | + "", |
| 45 | + ...formatTable(HEADERS, digits), |
| 46 | + "", |
| 47 | + ].join("\n"); |
| 48 | +} |
| 49 | + |
| 50 | +function code(value: string): string { |
| 51 | + const escaped = value.replaceAll("|", String.raw`\|`); |
| 52 | + |
| 53 | + if (escaped.includes("`")) { |
| 54 | + return `\`\` ${escaped} \`\``; |
| 55 | + } |
| 56 | + |
| 57 | + return `\`${escaped}\``; |
| 58 | +} |
| 59 | + |
| 60 | +function formatTable( |
| 61 | + headers: readonly string[], |
| 62 | + rows: readonly string[][], |
| 63 | +): string[] { |
| 64 | + const columnWidths = headers.map((header, index) => |
| 65 | + Math.max(header.length, ...rows.map((row) => row[index].length)), |
| 66 | + ); |
| 67 | + const formatRow = (row: readonly string[]) => |
| 68 | + `| ${row |
| 69 | + .map( |
| 70 | + (cell, index) => |
| 71 | + `${cell}${" ".repeat(columnWidths[index] - cell.length)}`, |
| 72 | + ) |
| 73 | + .join(" | ")} |`; |
| 74 | + |
| 75 | + return [ |
| 76 | + formatRow(headers), |
| 77 | + formatRow(columnWidths.map((width) => "-".repeat(width))), |
| 78 | + ...rows.map(formatRow), |
| 79 | + ]; |
| 80 | +} |
0 commit comments