Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
import { stripFrontmatter } from '../extensions/frontmatter.ts';
import { spellLineEndings } from './line-endings.ts';
import type { BridgeToleranceClass } from './normalize.ts';
import { isSubsequence } from './subsequence.ts';
export const PARSE_EQUIVALENCE_TOLERANCE = 'parse-equivalence' as const;
export type BridgeToleranceSignal = BridgeToleranceClass | typeof PARSE_EQUIVALENCE_TOLERANCE;
function comparableBody(body: string): string {
return stripDocBoundary(stripTrailingLineWhitespace(spellLineEndings(body, '\n')));
}
function stripDocBoundary(body: string): string {
return body.replace(/^\n+/, '').replace(/\n+$/, '');
}
function stripTrailingLineWhitespace(body: string): string {
return body.replace(/[ \t]+$/gm, '');
}
const ORDERED_MARKER_DIGITS_RE = /^([ \t]*)\d+([.)])(?=[ \t])/;
function contentSkeleton(body: string): string {
return body
.split('\n')
.map((line) =>
line.replace(ORDERED_MARKER_DIGITS_RE, (_m, indent, delim) => `${indent}1${delim}`),
)
.join('')
.replace(/\s+/g, '');
}
const warnedCanonicalizeErrors = new Set<string>();
const MAX_WARNED_CANONICALIZE_ERRORS = 8;
export function isParseEquivalentBridge(
left: string,
right: string,
canonicalizeBody: (body: string) => string,
): boolean {
const leftSplit = stripFrontmatter(left);
const rightSplit = stripFrontmatter(right);
if (leftSplit.frontmatter !== rightSplit.frontmatter) return false;
if (leftSplit.body === rightSplit.body) return true;
let canonicalLeftBody: string;
try {
canonicalLeftBody = canonicalizeBody(leftSplit.body);
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
if (
warnedCanonicalizeErrors.size < MAX_WARNED_CANONICALIZE_ERRORS &&
!warnedCanonicalizeErrors.has(message)
) {
warnedCanonicalizeErrors.add(message);
console.warn(
'[parse-equivalence] canonicalizeBody threw; treating as not equivalent:',
message,
);
}
return false;
}
if (comparableBody(canonicalLeftBody) !== comparableBody(rightSplit.body)) return false;
return isSubsequence(contentSkeleton(leftSplit.body), contentSkeleton(canonicalLeftBody));
}