master @ 35 LINES
[ HISTORY ] [ UP ]
┌─ TS ───────────────────────────────────────────────────────────────────────┐
│ // Framed unified diff for use outside markdown - the component sibling │
│ // of the ```diff fence. Shares the line classifier with the pipeline. │
│ import { checkGrid, len } from "../../lib/ascii"; │
│ import { diffLineClass } from "../../lib/rehype-ascii"; │
│ │
│ export interface Props { │
│ code: string; │
│ } │
│ │
│ export interface DiffRow { │
│ text: string; │
│ cls: string | null; │
│ /** Trailing pad cells before the right border. */ │
│ pad: number; │
│ } │
│ │
│ export function layoutDiff(code: string, width: number, classify = true): DiffRow[ │
│ ] { │
│ const inner = width - 4; │
│ return code │
│ .replace(/\n+$/, "") │
│ .split("\n") │
│ .flatMap((line) => { │
│ const cls = classify ? diffLineClass(line) : null; │
│ const cps = [...line]; │
│ const chunks = cps.length <= inner │
│ ? [line] │
│ : Array.from({ length: Math.ceil(cps.length / inner) }, (_, i) => │
│ cps.slice(i * inner, (i + 1) * inner).join("")); │
│ return chunks.map((text) => { │
│ checkGrid(text, inner, "diff row"); │
│ return { text, cls, pad: inner - len(text) }; │
│ }); │
│ }); │
│ } │
└────────────────────────────────────────────────────────────────────────────────────┘
┌─ TS ─────────────────────────────────┐
│ // Framed unified diff for use outside markd │
│ own - the component sibling │
│ // of the ```diff fence. Shares the line cla │
│ ssifier with the pipeline. │
│ import { checkGrid, len } from "../../lib/as │
│ cii"; │
│ import { diffLineClass } from "../../lib/reh │
│ ype-ascii"; │
│ │
│ export interface Props { │
│ code: string; │
│ } │
│ │
│ export interface DiffRow { │
│ text: string; │
│ cls: string | null; │
│ /** Trailing pad cells before the right bo │
│ rder. */ │
│ pad: number; │
│ } │
│ │
│ export function layoutDiff(code: string, wid │
│ th: number, classify = true): DiffRow[] { │
│ const inner = width - 4; │
│ return code │
│ .replace(/\n+$/, "") │
│ .split("\n") │
│ .flatMap((line) => { │
│ const cls = classify ? diffLineClass(l │
│ ine) : null; │
│ const cps = [...line]; │
│ const chunks = cps.length <= inner │
│ ? [line] │
│ : Array.from({ length: Math.ceil(cps │
│ .length / inner) }, (_, i) => │
│ cps.slice(i * inner, (i + 1) * i │
│ nner).join("")); │
│ return chunks.map((text) => { │
│ checkGrid(text, inner, "diff row"); │
│ return { text, cls, pad: inner - len │
│ (text) }; │
│ }); │
│ }); │
│ } │
└──────────────────────────────────────────────┘
──────────────────────────────────────────────────────────────────────────────────────
OOKNET
────────────────────────────────────────────────
OOKNET