@bendyline/squisq 1.5.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +63 -10
- package/dist/{Doc-BBVGq1_9.d.ts → Doc-ylqWKDc_.d.ts} +248 -37
- package/dist/{ImageEditDoc-FuyGtt6o.d.ts → ImageEditDoc-D828IXJ1.d.ts} +1 -1
- package/dist/annotationCoercion-Dr_DY9Hx.d.ts +86 -0
- package/dist/{chunk-VWUFZ6ZG.js → chunk-3E5F2XMR.js} +2 -29
- package/dist/chunk-3E5F2XMR.js.map +1 -0
- package/dist/chunk-4X3JQXNM.js +35 -0
- package/dist/chunk-4X3JQXNM.js.map +1 -0
- package/dist/{chunk-P3K7NLHF.js → chunk-5DKBFXPY.js} +31 -121
- package/dist/chunk-5DKBFXPY.js.map +1 -0
- package/dist/{chunk-DLXZMS5K.js → chunk-5J4HKRJF.js} +51 -3
- package/dist/chunk-5J4HKRJF.js.map +1 -0
- package/dist/chunk-5UYI3RJB.js +2503 -0
- package/dist/chunk-5UYI3RJB.js.map +1 -0
- package/dist/{chunk-BEQODJWV.js → chunk-66QO7XFB.js} +11 -291
- package/dist/chunk-66QO7XFB.js.map +1 -0
- package/dist/{chunk-KKNUBQ6Y.js → chunk-6PJRB2CF.js} +156 -91
- package/dist/chunk-6PJRB2CF.js.map +1 -0
- package/dist/{chunk-HWVFJAAH.js → chunk-AMIR72JP.js} +28 -10
- package/dist/chunk-AMIR72JP.js.map +1 -0
- package/dist/chunk-FVJUUTEM.js +74 -0
- package/dist/chunk-FVJUUTEM.js.map +1 -0
- package/dist/{chunk-5TYS5V3G.js → chunk-GF3BVNLU.js} +4127 -3538
- package/dist/chunk-GF3BVNLU.js.map +1 -0
- package/dist/{chunk-Q3ROPT5H.js → chunk-GZY66SDN.js} +1 -1
- package/dist/{chunk-Q3ROPT5H.js.map → chunk-GZY66SDN.js.map} +1 -1
- package/dist/chunk-JOQYPRDZ.js +127 -0
- package/dist/chunk-JOQYPRDZ.js.map +1 -0
- package/dist/{chunk-A4O7GIWE.js → chunk-LYNJEOAC.js} +10 -73
- package/dist/chunk-LYNJEOAC.js.map +1 -0
- package/dist/{chunk-LDQ2HJIX.js → chunk-PXLVXTLW.js} +252 -54
- package/dist/chunk-PXLVXTLW.js.map +1 -0
- package/dist/{chunk-TQWLI6S2.js → chunk-Q7LROV2H.js} +4 -2
- package/dist/{chunk-TQWLI6S2.js.map → chunk-Q7LROV2H.js.map} +1 -1
- package/dist/{chunk-T5UK6YOB.js → chunk-QTSJOY54.js} +60 -17
- package/dist/chunk-QTSJOY54.js.map +1 -0
- package/dist/chunk-QVANWDPH.js +2358 -0
- package/dist/chunk-QVANWDPH.js.map +1 -0
- package/dist/{chunk-GPLTCSXO.js → chunk-R2GC6RLD.js} +7 -7
- package/dist/chunk-R2GC6RLD.js.map +1 -0
- package/dist/chunk-R2YZTCLO.js +292 -0
- package/dist/chunk-R2YZTCLO.js.map +1 -0
- package/dist/chunk-S7A5D7ZJ.js +174 -0
- package/dist/chunk-S7A5D7ZJ.js.map +1 -0
- package/dist/chunk-T3TI7GSZ.js +884 -0
- package/dist/chunk-T3TI7GSZ.js.map +1 -0
- package/dist/{chunk-LTN6I7QW.js → chunk-TUWMK62B.js} +55 -4
- package/dist/chunk-TUWMK62B.js.map +1 -0
- package/dist/{chunk-IMSCRLLV.js → chunk-VR7UPJ7C.js} +35 -25
- package/dist/chunk-VR7UPJ7C.js.map +1 -0
- package/dist/doc/index.d.ts +2334 -5
- package/dist/doc/index.js +127 -33
- package/dist/generate/index.d.ts +7 -2
- package/dist/generate/index.js +1 -1
- package/dist/imageEdit/index.d.ts +9 -6
- package/dist/imageEdit/index.js +3 -3
- package/dist/index.d.ts +12 -10
- package/dist/index.js +246 -80
- package/dist/jsonForm/index.d.ts +7 -3
- package/dist/jsonForm/index.js +9 -4
- package/dist/markdown/index.d.ts +6 -70
- package/dist/markdown/index.js +13 -9
- package/dist/narration/index.d.ts +618 -0
- package/dist/narration/index.js +82 -0
- package/dist/recommend/index.d.ts +15 -1
- package/dist/recommend/index.js +2 -2
- package/dist/schemas/index.d.ts +28 -6
- package/dist/schemas/index.js +15 -13
- package/dist/spatial/index.d.ts +0 -1
- package/dist/spatial/index.js +1 -1
- package/dist/storage/index.js +1 -1
- package/dist/{themeLibrary-CehcJhzz.d.ts → themeLibrary-RjVgLpUi.d.ts} +10 -11
- package/dist/timing/index.js +5 -3
- package/dist/transform/index.d.ts +35 -24
- package/dist/transform/index.js +9 -10
- package/dist/{types-DlAZ7MW4.d.ts → types-BwMPl2CB.d.ts} +1 -1
- package/dist/{types-D98li1gV.d.ts → types-C4kiRn_k.d.ts} +3 -1
- package/dist/versions/index.d.ts +2 -1
- package/dist/versions/index.js +2 -2
- package/package.json +6 -6
- package/src/__tests__/applyNarrationTiming.test.ts +190 -0
- package/src/__tests__/applyRenderStyle.test.ts +3 -3
- package/src/__tests__/asciiDiagramBattle.test.ts +246 -0
- package/src/__tests__/asciiDiagramDetect.test.ts +162 -0
- package/src/__tests__/asciiDiagramMapping.test.ts +38 -0
- package/src/__tests__/asciiDiagramParse.test.ts +233 -0
- package/src/__tests__/asciiDiagramPipeline.test.ts +273 -0
- package/src/__tests__/asciiDiagramRender.test.ts +162 -0
- package/src/__tests__/asciiDiagramRepair.test.ts +84 -0
- package/src/__tests__/asciiTimelineDetect.test.ts +157 -0
- package/src/__tests__/asciiTimelineParse.test.ts +205 -0
- package/src/__tests__/asciiTimelinePipeline.test.ts +349 -0
- package/src/__tests__/asciiTimelineRender.test.ts +376 -0
- package/src/__tests__/attrTokens.test.ts +5 -0
- package/src/__tests__/contentContainer.test.ts +43 -0
- package/src/__tests__/customTemplatesFrontmatter.test.ts +0 -15
- package/src/__tests__/dataTable.test.ts +25 -15
- package/src/__tests__/diagramBlock.test.ts +3 -3
- package/src/__tests__/diagramText.test.ts +26 -0
- package/src/__tests__/docToMarkdownMedia.test.ts +83 -0
- package/src/__tests__/drawingBlock.test.ts +3 -3
- package/src/__tests__/fixtures/asciiDiagramBattle.ts +512 -0
- package/src/__tests__/fixtures/asciiDiagrams.ts +287 -0
- package/src/__tests__/fixtures/asciiTimelines.ts +35 -0
- package/src/__tests__/fixtures/treeviewFixtures.ts +243 -0
- package/src/__tests__/getLayers.test.ts +32 -18
- package/src/__tests__/getLayersFallback.test.ts +8 -8
- package/src/__tests__/imageEditVersions.test.ts +2 -0
- package/src/__tests__/jsonForm.pointer.test.ts +47 -1
- package/src/__tests__/layoutBlock.test.ts +3 -3
- package/src/__tests__/majorApiTypes.test.ts +18 -0
- package/src/__tests__/markdown.test.ts +41 -0
- package/src/__tests__/matchFontFamily.test.ts +114 -0
- package/src/__tests__/materializeBlockLayers.test.ts +135 -0
- package/src/__tests__/materializeTestUtils.ts +17 -0
- package/src/__tests__/narrationAlign.test.ts +150 -0
- package/src/__tests__/narrationFeatures.test.ts +71 -0
- package/src/__tests__/narrationNuclei.test.ts +45 -0
- package/src/__tests__/narrationPacing.test.ts +189 -0
- package/src/__tests__/narrationScript.test.ts +149 -0
- package/src/__tests__/narrationSessionRepro.test.ts +62 -0
- package/src/__tests__/narrationSidecar.test.ts +88 -0
- package/src/__tests__/narrationSyllables.test.ts +65 -0
- package/src/__tests__/narrationTestSignals.ts +167 -0
- package/src/__tests__/narrationVad.test.ts +66 -0
- package/src/__tests__/resolveDocTheme.test.ts +16 -2
- package/src/__tests__/rootExports.test.ts +18 -0
- package/src/__tests__/scopedContentContainer.test.ts +20 -0
- package/src/__tests__/shapeGeometry.test.ts +12 -0
- package/src/__tests__/templateAnnotationParse.test.ts +10 -0
- package/src/__tests__/templateInputs.test.ts +69 -0
- package/src/__tests__/templateMetadata.test.ts +2 -1
- package/src/__tests__/templates.test.ts +143 -17
- package/src/__tests__/themeCompile.test.ts +49 -22
- package/src/__tests__/transformProvenance.test.ts +160 -0
- package/src/__tests__/transformV2.test.ts +40 -14
- package/src/__tests__/treeviewBattle.test.ts +76 -0
- package/src/__tests__/treeviewDetect.test.ts +168 -0
- package/src/__tests__/treeviewParse.test.ts +118 -0
- package/src/__tests__/treeviewPipeline.test.ts +183 -0
- package/src/__tests__/treeviewRender.test.ts +91 -0
- package/src/__tests__/versions.test.ts +44 -0
- package/src/doc/applyNarrationTiming.ts +244 -0
- package/src/doc/asciiDiagram/chars.ts +210 -0
- package/src/doc/asciiDiagram/detect.ts +175 -0
- package/src/doc/asciiDiagram/index.ts +38 -0
- package/src/doc/asciiDiagram/mapping.ts +225 -0
- package/src/doc/asciiDiagram/parse.ts +1095 -0
- package/src/doc/asciiDiagram/render.ts +963 -0
- package/src/doc/asciiDiagram/repair.ts +400 -0
- package/src/doc/asciiDiagram/types.ts +65 -0
- package/src/doc/asciiTimeline/detect.ts +138 -0
- package/src/doc/asciiTimeline/index.ts +26 -0
- package/src/doc/asciiTimeline/mapping.ts +43 -0
- package/src/doc/asciiTimeline/parse.ts +870 -0
- package/src/doc/asciiTimeline/render.ts +427 -0
- package/src/doc/asciiTimeline/types.ts +73 -0
- package/src/doc/asciiTimeline/wrappedFlow.ts +427 -0
- package/src/doc/audioMapping.ts +60 -46
- package/src/doc/customTemplatesFrontmatter.ts +0 -41
- package/src/doc/docToMarkdown.ts +70 -2
- package/src/doc/index.ts +76 -4
- package/src/doc/markdownToDoc.ts +135 -1
- package/src/doc/materializeBlockLayers.ts +304 -0
- package/src/doc/mediaAnnotations.ts +20 -0
- package/src/doc/resolveDocTheme.ts +8 -7
- package/src/doc/templateInputs.ts +185 -2
- package/src/doc/templates/__tests__/customTemplateRegistry.test.ts +22 -8
- package/src/doc/templates/diagramBlock.ts +237 -38
- package/src/doc/templates/fallbackBlock.ts +1 -1
- package/src/doc/templates/index.ts +134 -279
- package/src/doc/templates/inputDescriptors.ts +20 -1
- package/src/doc/templates/metadata.ts +10 -0
- package/src/doc/templates/persistentLayers.ts +12 -126
- package/src/doc/templates/registry.ts +88 -0
- package/src/doc/templates/templateNames.ts +22 -0
- package/src/doc/templates/timelineBlock.ts +613 -0
- package/src/doc/templates/treeBlock.ts +131 -0
- package/src/doc/treeview/chars.ts +72 -0
- package/src/doc/treeview/detect.ts +145 -0
- package/src/doc/treeview/index.ts +30 -0
- package/src/doc/treeview/mapping.ts +95 -0
- package/src/doc/treeview/parse.ts +201 -0
- package/src/doc/treeview/render.ts +41 -0
- package/src/doc/treeview/types.ts +43 -0
- package/src/doc/utils/applyRenderStyle.ts +2 -9
- package/src/doc/utils/diagramText.ts +78 -0
- package/src/doc/utils/shapeGeometry.ts +67 -5
- package/src/doc/validate.ts +1 -1
- package/src/generate/templateMapper.ts +7 -0
- package/src/imageEdit/export.ts +6 -5
- package/src/imageEdit/versionPaths.ts +13 -26
- package/src/imageEdit/versions.ts +21 -100
- package/src/index.ts +1 -0
- package/src/internal/immutable.ts +58 -0
- package/src/jsonForm/index.ts +9 -1
- package/src/jsonForm/pointer.ts +68 -23
- package/src/markdown/attrTokens.ts +7 -5
- package/src/markdown/index.ts +1 -0
- package/src/markdown/stringify.ts +4 -9
- package/src/markdown/utils.ts +36 -14
- package/src/narration/align.ts +514 -0
- package/src/narration/features.ts +231 -0
- package/src/narration/index.ts +86 -0
- package/src/narration/nuclei.ts +118 -0
- package/src/narration/pacing.ts +211 -0
- package/src/narration/script.ts +239 -0
- package/src/narration/session.ts +100 -0
- package/src/narration/sidecar.ts +187 -0
- package/src/narration/syllables.ts +85 -0
- package/src/narration/trace.ts +50 -0
- package/src/narration/types.ts +299 -0
- package/src/narration/vad.ts +131 -0
- package/src/recommend/numberHighlight.ts +25 -0
- package/src/recommend/templates.ts +108 -12
- package/src/schemas/BlockTemplates.ts +156 -7
- package/src/schemas/Doc.ts +72 -9
- package/src/schemas/Media.ts +12 -2
- package/src/schemas/Theme.ts +45 -25
- package/src/schemas/Viewport.ts +2 -2
- package/src/schemas/fontStacks.ts +79 -0
- package/src/schemas/themeCompile.ts +15 -0
- package/src/schemas/themeConstants.ts +2 -0
- package/src/schemas/themeLibrary.ts +25 -19
- package/src/schemas/themeValidator.ts +1 -1
- package/src/spatial/Geohash.ts +0 -3
- package/src/storage/ContentContainer.ts +17 -9
- package/src/storage/ScopedContentContainer.ts +45 -15
- package/src/transform/applyTransform.ts +5 -5
- package/src/transform/index.ts +3 -1
- package/src/transform/registry.ts +169 -49
- package/src/transform/templateSelector.ts +21 -2
- package/src/transform/timingAllocator.ts +152 -21
- package/src/transform/types.ts +14 -1
- package/src/versions/gitignore.ts +34 -0
- package/src/versions/index.ts +2 -0
- package/src/versions/operations.ts +18 -104
- package/src/versions/paths.ts +13 -28
- package/src/versions/snapshotStore.ts +202 -0
- package/src/versions/types.ts +3 -1
- package/dist/chunk-5TYS5V3G.js.map +0 -1
- package/dist/chunk-A4O7GIWE.js.map +0 -1
- package/dist/chunk-BEQODJWV.js.map +0 -1
- package/dist/chunk-DLXZMS5K.js.map +0 -1
- package/dist/chunk-GPLTCSXO.js.map +0 -1
- package/dist/chunk-HWVFJAAH.js.map +0 -1
- package/dist/chunk-IMSCRLLV.js.map +0 -1
- package/dist/chunk-JELIRVDP.js +0 -24
- package/dist/chunk-JELIRVDP.js.map +0 -1
- package/dist/chunk-KKNUBQ6Y.js.map +0 -1
- package/dist/chunk-LDQ2HJIX.js.map +0 -1
- package/dist/chunk-LTN6I7QW.js.map +0 -1
- package/dist/chunk-P3K7NLHF.js.map +0 -1
- package/dist/chunk-T5UK6YOB.js.map +0 -1
- package/dist/chunk-UY7KGQ5R.js +0 -192
- package/dist/chunk-UY7KGQ5R.js.map +0 -1
- package/dist/chunk-VLJJHDAC.js +0 -218
- package/dist/chunk-VLJJHDAC.js.map +0 -1
- package/dist/chunk-VWUFZ6ZG.js.map +0 -1
- package/dist/story/index.d.ts +0 -1801
- package/dist/story/index.js +0 -279
- package/src/doc/getLayers.ts +0 -195
- /package/dist/{story → narration}/index.js.map +0 -0
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { describe, expect, it, beforeEach } from 'vitest';
|
|
2
2
|
import { MemoryContentContainer } from '../storage/ContentContainer';
|
|
3
|
+
import { scopeContainer } from '../storage/ScopedContentContainer';
|
|
3
4
|
import {
|
|
4
5
|
DocumentVersionManager,
|
|
5
6
|
VERSIONS_PREFIX,
|
|
@@ -111,6 +112,49 @@ describe('saveVersion', () => {
|
|
|
111
112
|
expect(result.version?.path).toBe(`${VERSIONS_PREFIX}index.20260430T152030Z.md`);
|
|
112
113
|
expect(result.version?.basename).toBe('index');
|
|
113
114
|
expect(await readUtf8(container, result.version!.path)).toBe('# hello');
|
|
115
|
+
expect(await readUtf8(container, '.gitignore')).toBe('.versions/\n');
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('writes the ignore rule at the root of a scoped *_files sidecar', async () => {
|
|
119
|
+
const parent = new MemoryContentContainer();
|
|
120
|
+
const sidecar = scopeContainer(parent, 'notes_files');
|
|
121
|
+
const now = new Date(Date.UTC(2026, 3, 30, 15, 20, 30));
|
|
122
|
+
|
|
123
|
+
const result = await saveVersion(sidecar, {
|
|
124
|
+
basename: 'notes',
|
|
125
|
+
content: '# hello',
|
|
126
|
+
now,
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
expect(result.saved).toBe(true);
|
|
130
|
+
expect(await readUtf8(parent, 'notes_files/.gitignore')).toBe('.versions/\n');
|
|
131
|
+
expect(await parent.exists(`notes_files/${result.version!.path}`)).toBe(true);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('preserves existing .gitignore rules and does not duplicate the versions rule', async () => {
|
|
135
|
+
await container.writeDocument('# hello', 'index.md');
|
|
136
|
+
await container.writeFile(
|
|
137
|
+
'.gitignore',
|
|
138
|
+
new TextEncoder().encode('generated/\r\n'),
|
|
139
|
+
'text/plain',
|
|
140
|
+
);
|
|
141
|
+
await saveVersion(container, { now: new Date(Date.UTC(2026, 3, 30, 15, 20, 30)) });
|
|
142
|
+
await saveVersion(container, { now: new Date(Date.UTC(2026, 3, 30, 15, 20, 31)) });
|
|
143
|
+
|
|
144
|
+
expect(await readUtf8(container, '.gitignore')).toBe('generated/\r\n.versions/\r\n');
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('backfills .gitignore when an existing snapshot is unchanged', async () => {
|
|
148
|
+
await container.writeDocument('# hello', 'index.md');
|
|
149
|
+
await saveVersion(container, { now: new Date(Date.UTC(2026, 3, 30, 15, 20, 30)) });
|
|
150
|
+
await container.removeFile('.gitignore');
|
|
151
|
+
|
|
152
|
+
const result = await saveVersion(container, {
|
|
153
|
+
now: new Date(Date.UTC(2026, 3, 30, 15, 20, 31)),
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
expect(result.reason).toBe('unchanged');
|
|
157
|
+
expect(await readUtf8(container, '.gitignore')).toBe('.versions/\n');
|
|
114
158
|
});
|
|
115
159
|
|
|
116
160
|
it('returns unchanged on identical second save', async () => {
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Apply narration timing from a document-anchored take to a Doc's block
|
|
3
|
+
* timeline.
|
|
4
|
+
*
|
|
5
|
+
* When a doc carries a document-spanning narration clip (a preamble
|
|
6
|
+
* `{[audio src=… anchor=document]}` → `doc.documentMedia`) whose
|
|
7
|
+
* `.timing.json` sidecar has per-block ranges (v3, written by the
|
|
8
|
+
* teleprompter's aligner), the blocks are re-timed so playback and
|
|
9
|
+
* exports advance in sync with the recorded voice. Runs as step 0 of
|
|
10
|
+
* `resolveAudioMapping`, so every surface that resolves audio gets it.
|
|
11
|
+
*
|
|
12
|
+
* Precedence: author-pinned `duration=`/`startTime=` heading attrs win
|
|
13
|
+
* over narration ranges (a conflicting pin gets an `info` diagnostic);
|
|
14
|
+
* narration ranges win over per-block audio-segment mapping and
|
|
15
|
+
* reading-time estimates.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import type { Block, Doc, DocDiagnostic } from '../schemas/Doc.js';
|
|
19
|
+
import type { MediaClip } from '../schemas/Media.js';
|
|
20
|
+
import type { ContentContainer } from '../storage/ContentContainer.js';
|
|
21
|
+
import { estimateTimeFromText } from '../timing/narrationTiming.js';
|
|
22
|
+
import { buildNarrationScript } from '../narration/script.js';
|
|
23
|
+
import {
|
|
24
|
+
parseNarrationTimingJson,
|
|
25
|
+
type NarrationTimingBlock,
|
|
26
|
+
type NarrationTimingJsonV3,
|
|
27
|
+
} from '../narration/sidecar.js';
|
|
28
|
+
import { flattenRenderableBlocks, getBlockBodyText, getPinnedBlockMeta } from './markdownToDoc.js';
|
|
29
|
+
import { scoreTextSimilarity } from './audioMapping.js';
|
|
30
|
+
|
|
31
|
+
export interface NarrationResolution {
|
|
32
|
+
doc: Doc;
|
|
33
|
+
/** False → the doc is returned unchanged (no clip / no sidecar / no match). */
|
|
34
|
+
applied: boolean;
|
|
35
|
+
/** The narration media path, for callers that bundle assets. */
|
|
36
|
+
clipSrc?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Minimum similarity for the fuzzy block-matching rung. */
|
|
40
|
+
const MIN_BLOCK_SIMILARITY = 0.5;
|
|
41
|
+
/** Minimum whole-script similarity for the v1 proportional fallback. */
|
|
42
|
+
const MIN_V1_SCRIPT_SIMILARITY = 0.8;
|
|
43
|
+
/** Pins that disagree with the narration range by more than this get a diagnostic. */
|
|
44
|
+
const PIN_CONFLICT_TOLERANCE_SEC = 1;
|
|
45
|
+
|
|
46
|
+
interface BlockRangeSec {
|
|
47
|
+
startSec: number;
|
|
48
|
+
endSec: number;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Spoken text of a block, mirroring the narration script builder. */
|
|
52
|
+
function blockSpokenText(block: Block): string {
|
|
53
|
+
const title = block.title ? block.title.trim() : '';
|
|
54
|
+
const body = getBlockBodyText(block);
|
|
55
|
+
if (title && body) return `${title}\n${body}`;
|
|
56
|
+
return title || body;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Match sidecar block ranges to doc blocks with a deterministic ladder:
|
|
61
|
+
* exact blockId → text similarity (greedy best-first, ≥ 0.5) →
|
|
62
|
+
* in-order zip when the leftover counts agree. Unmatchable sidecar
|
|
63
|
+
* blocks are dropped.
|
|
64
|
+
*/
|
|
65
|
+
function matchSidecarBlocks(
|
|
66
|
+
flat: Block[],
|
|
67
|
+
timing: NarrationTimingJsonV3,
|
|
68
|
+
): Map<string, BlockRangeSec> {
|
|
69
|
+
const ranges = new Map<string, BlockRangeSec>();
|
|
70
|
+
const remainingBlocks = new Set(flat.map((b) => b.id));
|
|
71
|
+
const remainingSidecar: NarrationTimingBlock[] = [];
|
|
72
|
+
|
|
73
|
+
const byId = new Map(flat.map((b) => [b.id, b]));
|
|
74
|
+
for (const entry of timing.blocks) {
|
|
75
|
+
if (entry.blockId && byId.has(entry.blockId) && remainingBlocks.has(entry.blockId)) {
|
|
76
|
+
ranges.set(entry.blockId, { startSec: entry.startSec, endSec: entry.endSec });
|
|
77
|
+
remainingBlocks.delete(entry.blockId);
|
|
78
|
+
} else {
|
|
79
|
+
remainingSidecar.push(entry);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Fuzzy rung: greedy best pair first, each side used once.
|
|
84
|
+
if (remainingSidecar.length > 0 && remainingBlocks.size > 0) {
|
|
85
|
+
const candidates: Array<{ score: number; entry: NarrationTimingBlock; blockId: string }> = [];
|
|
86
|
+
for (const entry of remainingSidecar) {
|
|
87
|
+
const slice = timing.sourceText.slice(entry.charStart, entry.charEnd);
|
|
88
|
+
for (const block of flat) {
|
|
89
|
+
if (!remainingBlocks.has(block.id)) continue;
|
|
90
|
+
const score = scoreTextSimilarity(slice, blockSpokenText(block));
|
|
91
|
+
if (score >= MIN_BLOCK_SIMILARITY) candidates.push({ score, entry, blockId: block.id });
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
candidates.sort((a, b) => b.score - a.score || a.entry.blockIndex - b.entry.blockIndex);
|
|
95
|
+
const usedEntries = new Set<NarrationTimingBlock>();
|
|
96
|
+
for (const candidate of candidates) {
|
|
97
|
+
if (usedEntries.has(candidate.entry) || !remainingBlocks.has(candidate.blockId)) continue;
|
|
98
|
+
ranges.set(candidate.blockId, {
|
|
99
|
+
startSec: candidate.entry.startSec,
|
|
100
|
+
endSec: candidate.entry.endSec,
|
|
101
|
+
});
|
|
102
|
+
usedEntries.add(candidate.entry);
|
|
103
|
+
remainingBlocks.delete(candidate.blockId);
|
|
104
|
+
}
|
|
105
|
+
// Order rung: leftover counts equal → zip in document/sidecar order.
|
|
106
|
+
const leftoverEntries = remainingSidecar.filter((e) => !usedEntries.has(e));
|
|
107
|
+
if (leftoverEntries.length > 0 && leftoverEntries.length === remainingBlocks.size) {
|
|
108
|
+
const leftoverBlocks = flat.filter((b) => remainingBlocks.has(b.id));
|
|
109
|
+
leftoverEntries.sort((a, b) => a.blockIndex - b.blockIndex);
|
|
110
|
+
leftoverBlocks.forEach((block, i) => {
|
|
111
|
+
const entry = leftoverEntries[i];
|
|
112
|
+
ranges.set(block.id, { startSec: entry.startSec, endSec: entry.endSec });
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return ranges;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* v1 fallback: no block ranges in the sidecar. If the take's source
|
|
122
|
+
* text still narrates this doc (whole-script similarity ≥ 0.8), derive
|
|
123
|
+
* per-block ranges by spoken-word proportion.
|
|
124
|
+
*/
|
|
125
|
+
function proportionalRanges(
|
|
126
|
+
doc: Doc,
|
|
127
|
+
timing: NarrationTimingJsonV3,
|
|
128
|
+
): Map<string, BlockRangeSec> | null {
|
|
129
|
+
const script = buildNarrationScript(doc);
|
|
130
|
+
if (script.blocks.length === 0) return null;
|
|
131
|
+
if (scoreTextSimilarity(script.sourceText, timing.sourceText) < MIN_V1_SCRIPT_SIMILARITY) {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
const ranges = new Map<string, BlockRangeSec>();
|
|
135
|
+
const starts = script.blocks.map((range) =>
|
|
136
|
+
estimateTimeFromText(script.sourceText, range.charStart, timing.duration),
|
|
137
|
+
);
|
|
138
|
+
script.blocks.forEach((range, i) => {
|
|
139
|
+
ranges.set(range.blockId, {
|
|
140
|
+
startSec: starts[i],
|
|
141
|
+
endSec: i + 1 < starts.length ? starts[i + 1] : timing.duration,
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
return ranges;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
interface RetimeContext {
|
|
148
|
+
clipStart: number;
|
|
149
|
+
ranges: Map<string, BlockRangeSec>;
|
|
150
|
+
diagnostics: DocDiagnostic[];
|
|
151
|
+
/** Timeline cursor (doc seconds) for unmatched blocks. */
|
|
152
|
+
cursor: number;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Recursively clone blocks, applying narration ranges to matched, unpinned blocks. */
|
|
156
|
+
function retimeBlocks(blocks: Block[], ctx: RetimeContext): Block[] {
|
|
157
|
+
return blocks.map((block) => {
|
|
158
|
+
const range = ctx.ranges.get(block.id);
|
|
159
|
+
const pinned = getPinnedBlockMeta(block);
|
|
160
|
+
const next: Block = { ...block };
|
|
161
|
+
|
|
162
|
+
if (range && pinned.duration == null && pinned.startTime == null) {
|
|
163
|
+
next.startTime = ctx.clipStart + range.startSec;
|
|
164
|
+
next.duration = Math.max(0, range.endSec - range.startSec);
|
|
165
|
+
ctx.cursor = Math.max(ctx.cursor, next.startTime + next.duration);
|
|
166
|
+
} else if (range) {
|
|
167
|
+
// Author pins win; surface real disagreement as an info diagnostic.
|
|
168
|
+
const pinnedStart = pinned.startTime ?? block.startTime;
|
|
169
|
+
const pinnedDuration = pinned.duration ?? block.duration;
|
|
170
|
+
const narrStart = ctx.clipStart + range.startSec;
|
|
171
|
+
const narrDuration = Math.max(0, range.endSec - range.startSec);
|
|
172
|
+
if (
|
|
173
|
+
Math.abs(pinnedStart - narrStart) > PIN_CONFLICT_TOLERANCE_SEC ||
|
|
174
|
+
Math.abs(pinnedDuration - narrDuration) > PIN_CONFLICT_TOLERANCE_SEC
|
|
175
|
+
) {
|
|
176
|
+
ctx.diagnostics.push({
|
|
177
|
+
severity: 'info',
|
|
178
|
+
code: 'narration-pin-conflict',
|
|
179
|
+
message:
|
|
180
|
+
`Block timing is pinned (duration=/startTime=) but the recorded narration says ` +
|
|
181
|
+
`~${narrDuration.toFixed(1)}s starting at ~${narrStart.toFixed(1)}s. The pin wins; ` +
|
|
182
|
+
`remove it to follow the narration.`,
|
|
183
|
+
blockId: block.id,
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
ctx.cursor = Math.max(ctx.cursor, pinnedStart + pinnedDuration);
|
|
187
|
+
} else {
|
|
188
|
+
// No narration range (block added after the take, or unmatched):
|
|
189
|
+
// keep its duration but place it at the running cursor so the
|
|
190
|
+
// timeline stays monotonic.
|
|
191
|
+
next.startTime = ctx.cursor;
|
|
192
|
+
ctx.cursor += next.duration;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (block.children && block.children.length > 0) {
|
|
196
|
+
next.children = retimeBlocks(block.children, ctx);
|
|
197
|
+
}
|
|
198
|
+
return next;
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Find the doc's document-anchored narration clip, load its timing
|
|
204
|
+
* sidecar, and re-time the blocks from its ranges. Pure with respect to
|
|
205
|
+
* the input doc — returns a new Doc (or the original when nothing
|
|
206
|
+
* applies).
|
|
207
|
+
*/
|
|
208
|
+
export async function applyNarrationTiming(
|
|
209
|
+
doc: Doc,
|
|
210
|
+
container: ContentContainer,
|
|
211
|
+
): Promise<NarrationResolution> {
|
|
212
|
+
const clips: MediaClip[] = (doc.documentMedia ?? []).filter((c) => c.anchor === 'document');
|
|
213
|
+
for (const clip of clips) {
|
|
214
|
+
if (!clip.src) continue;
|
|
215
|
+
const sidecarData = await container.readFile(`${clip.src}.timing.json`);
|
|
216
|
+
if (!sidecarData) continue;
|
|
217
|
+
const timing = parseNarrationTimingJson(sidecarData);
|
|
218
|
+
if (!timing) continue;
|
|
219
|
+
|
|
220
|
+
const flat = flattenRenderableBlocks(doc.blocks);
|
|
221
|
+
const ranges =
|
|
222
|
+
timing.blocks.length > 0 ? matchSidecarBlocks(flat, timing) : proportionalRanges(doc, timing);
|
|
223
|
+
if (!ranges || ranges.size === 0) continue;
|
|
224
|
+
|
|
225
|
+
const ctx: RetimeContext = {
|
|
226
|
+
clipStart: clip.startAt,
|
|
227
|
+
ranges,
|
|
228
|
+
diagnostics: [],
|
|
229
|
+
cursor: 0,
|
|
230
|
+
};
|
|
231
|
+
const blocks = retimeBlocks(doc.blocks, ctx);
|
|
232
|
+
const duration = Math.max(clip.startAt + timing.duration, ctx.cursor);
|
|
233
|
+
const retimed: Doc = {
|
|
234
|
+
...doc,
|
|
235
|
+
blocks,
|
|
236
|
+
duration,
|
|
237
|
+
...(ctx.diagnostics.length > 0
|
|
238
|
+
? { diagnostics: [...(doc.diagnostics ?? []), ...ctx.diagnostics] }
|
|
239
|
+
: {}),
|
|
240
|
+
};
|
|
241
|
+
return { doc: retimed, applied: true, clipSrc: clip.src };
|
|
242
|
+
}
|
|
243
|
+
return { doc, applied: false };
|
|
244
|
+
}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Character classification for ASCII/Unicode box-and-line diagram art.
|
|
3
|
+
*
|
|
4
|
+
* The parse side accepts a superset (light/rounded/heavy/double Unicode
|
|
5
|
+
* box-drawing plus ASCII `+ - |` and both arrow families); the render
|
|
6
|
+
* side always emits one of the two `StyleVocab` tables below.
|
|
7
|
+
*
|
|
8
|
+
* Internal to the asciiDiagram module — not exported from the barrel.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const TL_CORNERS = new Set(['┌', '╭', '┏', '╔']);
|
|
12
|
+
const TR_CORNERS = new Set(['┐', '╮', '┓', '╗']);
|
|
13
|
+
const BL_CORNERS = new Set(['└', '╰', '┗', '╚']);
|
|
14
|
+
const BR_CORNERS = new Set(['┘', '╯', '┛', '╝']);
|
|
15
|
+
const HORIZ = new Set(['─', '━', '═', '-']);
|
|
16
|
+
const VERT = new Set(['│', '┃', '║', '|']);
|
|
17
|
+
const JUNCTIONS = new Set([
|
|
18
|
+
'├',
|
|
19
|
+
'┤',
|
|
20
|
+
'┬',
|
|
21
|
+
'┴',
|
|
22
|
+
'┼',
|
|
23
|
+
'┣',
|
|
24
|
+
'┫',
|
|
25
|
+
'┳',
|
|
26
|
+
'┻',
|
|
27
|
+
'╋',
|
|
28
|
+
'╠',
|
|
29
|
+
'╣',
|
|
30
|
+
'╦',
|
|
31
|
+
'╩',
|
|
32
|
+
'╬',
|
|
33
|
+
'╞',
|
|
34
|
+
'╡',
|
|
35
|
+
'╥',
|
|
36
|
+
'╨',
|
|
37
|
+
'╫',
|
|
38
|
+
'╪',
|
|
39
|
+
]);
|
|
40
|
+
/** Junctions whose stems include a vertical component (legal on side borders). */
|
|
41
|
+
const SIDE_JUNCTIONS = new Set(['├', '┤', '┼', '┣', '┫', '╋', '╠', '╣', '╬', '╞', '╡', '╫', '╪']);
|
|
42
|
+
/** Junctions whose stems include a horizontal component (legal on top/bottom borders). */
|
|
43
|
+
const FLAT_JUNCTIONS = new Set(['┬', '┴', '┼', '┳', '┻', '╋', '╦', '╩', '╬', '╥', '╨', '╫', '╪']);
|
|
44
|
+
|
|
45
|
+
const ARROW_UP = new Set(['▲', '↑']);
|
|
46
|
+
const ARROW_DOWN = new Set(['▼', '↓']);
|
|
47
|
+
const ARROW_LEFT = new Set(['◄', '←', '◀']);
|
|
48
|
+
const ARROW_RIGHT = new Set(['►', '→', '▶']);
|
|
49
|
+
|
|
50
|
+
export type ArrowDirection = 'up' | 'down' | 'left' | 'right';
|
|
51
|
+
|
|
52
|
+
export function isTlCorner(ch: string): boolean {
|
|
53
|
+
return TL_CORNERS.has(ch);
|
|
54
|
+
}
|
|
55
|
+
export function isTrCorner(ch: string): boolean {
|
|
56
|
+
return TR_CORNERS.has(ch);
|
|
57
|
+
}
|
|
58
|
+
export function isBlCorner(ch: string): boolean {
|
|
59
|
+
return BL_CORNERS.has(ch);
|
|
60
|
+
}
|
|
61
|
+
export function isBrCorner(ch: string): boolean {
|
|
62
|
+
return BR_CORNERS.has(ch);
|
|
63
|
+
}
|
|
64
|
+
export function isAnyCorner(ch: string): boolean {
|
|
65
|
+
return TL_CORNERS.has(ch) || TR_CORNERS.has(ch) || BL_CORNERS.has(ch) || BR_CORNERS.has(ch);
|
|
66
|
+
}
|
|
67
|
+
export function isHoriz(ch: string): boolean {
|
|
68
|
+
return HORIZ.has(ch);
|
|
69
|
+
}
|
|
70
|
+
export function isVert(ch: string): boolean {
|
|
71
|
+
return VERT.has(ch);
|
|
72
|
+
}
|
|
73
|
+
export function isJunction(ch: string): boolean {
|
|
74
|
+
return JUNCTIONS.has(ch);
|
|
75
|
+
}
|
|
76
|
+
export function isPlus(ch: string): boolean {
|
|
77
|
+
return ch === '+';
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Acceptable interior of a top/bottom border walk (corners excluded). */
|
|
81
|
+
export function isFlatBorderChar(ch: string): boolean {
|
|
82
|
+
return isHoriz(ch) || FLAT_JUNCTIONS.has(ch) || isPlus(ch);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Acceptable interior of a left/right border walk (corners excluded). */
|
|
86
|
+
export function isSideBorderChar(ch: string): boolean {
|
|
87
|
+
return isVert(ch) || SIDE_JUNCTIONS.has(ch) || isPlus(ch);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Direction of an unconditional (Unicode) arrowhead, or null. ASCII
|
|
92
|
+
* `^ v < >` are ambiguous with prose and must be gated on an adjacent
|
|
93
|
+
* line character by the caller — use {@link asciiArrowDirection}.
|
|
94
|
+
*/
|
|
95
|
+
export function arrowDirection(ch: string): ArrowDirection | null {
|
|
96
|
+
if (ARROW_UP.has(ch)) return 'up';
|
|
97
|
+
if (ARROW_DOWN.has(ch)) return 'down';
|
|
98
|
+
if (ARROW_LEFT.has(ch)) return 'left';
|
|
99
|
+
if (ARROW_RIGHT.has(ch)) return 'right';
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Direction for ASCII arrow candidates (`^ v V < >`); caller applies the adjacency gate. */
|
|
104
|
+
export function asciiArrowDirection(ch: string): ArrowDirection | null {
|
|
105
|
+
if (ch === '^') return 'up';
|
|
106
|
+
if (ch === 'v' || ch === 'V') return 'down';
|
|
107
|
+
if (ch === '<') return 'left';
|
|
108
|
+
if (ch === '>') return 'right';
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** True for any character that can be part of an edge polyline (lines, turns, junctions). */
|
|
113
|
+
export function isLineChar(ch: string): boolean {
|
|
114
|
+
return isHoriz(ch) || isVert(ch) || isJunction(ch) || isPlus(ch) || isAnyCorner(ch);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** True for structural Unicode box-drawing characters (used for the style vote). */
|
|
118
|
+
export function isUnicodeStructural(ch: string): boolean {
|
|
119
|
+
const code = ch.codePointAt(0) ?? 0;
|
|
120
|
+
// Box Drawing block U+2500–U+257F plus the arrow glyphs we emit.
|
|
121
|
+
return (
|
|
122
|
+
(code >= 0x2500 && code <= 0x257f) ||
|
|
123
|
+
ARROW_UP.has(ch) ||
|
|
124
|
+
ARROW_DOWN.has(ch) ||
|
|
125
|
+
ch === '←' ||
|
|
126
|
+
ch === '→' ||
|
|
127
|
+
ch === '◄' ||
|
|
128
|
+
ch === '►' ||
|
|
129
|
+
ch === '◀' ||
|
|
130
|
+
ch === '▶'
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** True for structural ASCII drawing characters (used for the style vote). */
|
|
135
|
+
export function isAsciiStructural(ch: string): boolean {
|
|
136
|
+
return ch === '+' || ch === '-' || ch === '|';
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export interface StyleVocab {
|
|
140
|
+
tl: string;
|
|
141
|
+
tr: string;
|
|
142
|
+
bl: string;
|
|
143
|
+
br: string;
|
|
144
|
+
h: string;
|
|
145
|
+
v: string;
|
|
146
|
+
/** Horizontal border cell with a stem descending below it (e.g. a line leaving a bottom border). */
|
|
147
|
+
stemDown: string;
|
|
148
|
+
/** Horizontal border cell with a stem ascending above it (e.g. a line arriving at a top border). */
|
|
149
|
+
stemUp: string;
|
|
150
|
+
/** Vertical border cell with a stem extending leftward. */
|
|
151
|
+
stemLeft: string;
|
|
152
|
+
/** Vertical border cell with a stem extending rightward. */
|
|
153
|
+
stemRight: string;
|
|
154
|
+
cross: string;
|
|
155
|
+
arrowUp: string;
|
|
156
|
+
arrowDown: string;
|
|
157
|
+
arrowLeft: string;
|
|
158
|
+
arrowRight: string;
|
|
159
|
+
/** Turn glyphs for routed edges: connects the two named directions. */
|
|
160
|
+
turnDownRight: string;
|
|
161
|
+
turnDownLeft: string;
|
|
162
|
+
turnUpRight: string;
|
|
163
|
+
turnUpLeft: string;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export const UNICODE_VOCAB: StyleVocab = {
|
|
167
|
+
tl: '┌',
|
|
168
|
+
tr: '┐',
|
|
169
|
+
bl: '└',
|
|
170
|
+
br: '┘',
|
|
171
|
+
h: '─',
|
|
172
|
+
v: '│',
|
|
173
|
+
stemDown: '┬',
|
|
174
|
+
stemUp: '┴',
|
|
175
|
+
stemLeft: '┤',
|
|
176
|
+
stemRight: '├',
|
|
177
|
+
cross: '┼',
|
|
178
|
+
arrowUp: '▲',
|
|
179
|
+
arrowDown: '▼',
|
|
180
|
+
arrowLeft: '◄',
|
|
181
|
+
arrowRight: '►',
|
|
182
|
+
// Named by the pair of open sides: e.g. turnDownRight opens downward and
|
|
183
|
+
// to the right (a line coming from below turning to the right) = '┌'.
|
|
184
|
+
turnDownRight: '┌',
|
|
185
|
+
turnDownLeft: '┐',
|
|
186
|
+
turnUpRight: '└',
|
|
187
|
+
turnUpLeft: '┘',
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
export const ASCII_VOCAB: StyleVocab = {
|
|
191
|
+
tl: '+',
|
|
192
|
+
tr: '+',
|
|
193
|
+
bl: '+',
|
|
194
|
+
br: '+',
|
|
195
|
+
h: '-',
|
|
196
|
+
v: '|',
|
|
197
|
+
stemDown: '+',
|
|
198
|
+
stemUp: '+',
|
|
199
|
+
stemLeft: '+',
|
|
200
|
+
stemRight: '+',
|
|
201
|
+
cross: '+',
|
|
202
|
+
arrowUp: '^',
|
|
203
|
+
arrowDown: 'v',
|
|
204
|
+
arrowLeft: '<',
|
|
205
|
+
arrowRight: '>',
|
|
206
|
+
turnDownRight: '+',
|
|
207
|
+
turnDownLeft: '+',
|
|
208
|
+
turnUpRight: '+',
|
|
209
|
+
turnUpLeft: '+',
|
|
210
|
+
};
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ASCII diagram detection: is this fence content a box-and-line diagram?
|
|
3
|
+
*
|
|
4
|
+
* False positives are the expensive failure (they hide real code behind a
|
|
5
|
+
* canvas / template); false negatives just leave a code block alone. Every
|
|
6
|
+
* rejection path records a machine-greppable reason so the corpus tests
|
|
7
|
+
* act as the contract for any future threshold tuning.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { MarkdownCodeBlock } from '../../markdown/types.js';
|
|
11
|
+
import { parseAsciiDiagramWithStats } from './parse.js';
|
|
12
|
+
import type { AsciiDiagramDetection } from './types.js';
|
|
13
|
+
|
|
14
|
+
/** Fence languages eligible for diagram detection (plus no language at all). */
|
|
15
|
+
export const ASCII_DIAGRAM_FENCE_LANGS: ReadonlySet<string> = new Set([
|
|
16
|
+
'text',
|
|
17
|
+
'txt',
|
|
18
|
+
'plaintext',
|
|
19
|
+
'plain',
|
|
20
|
+
'ascii',
|
|
21
|
+
'diagram',
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
const MAX_LINES = 400;
|
|
25
|
+
const MAX_COLS = 400;
|
|
26
|
+
const MIN_LINES = 3;
|
|
27
|
+
const MAX_LOOSE_RATIO = 0.25;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Box-drawing corners, junctions, and (Unicode) verticals — characters that
|
|
31
|
+
* signal a box border leaked into a node's label, i.e. a mis-aligned parse.
|
|
32
|
+
* ASCII `|`/`+`/`-` and the horizontal `─` are excluded: they occur in real
|
|
33
|
+
* label text (`stdin | stdout`, `read-only`, `A + B`).
|
|
34
|
+
*/
|
|
35
|
+
const STRUCTURAL_IN_LABEL = /[│┃║┌┐└┘├┤┬┴┼╭╮╰╯┏┓┗┛╔╗╚╝┣┫┳┻╋╠╣╦╩╬╞╡╥╨╫╪]/;
|
|
36
|
+
|
|
37
|
+
export function isEligibleAsciiFenceLang(lang: string | null | undefined): boolean {
|
|
38
|
+
if (lang === null || lang === undefined) return true;
|
|
39
|
+
const normalized = lang.trim().toLowerCase();
|
|
40
|
+
return normalized === '' || ASCII_DIAGRAM_FENCE_LANGS.has(normalized);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* True when the fence LANGUAGE is the explicit `diagram` tag — an author-set
|
|
45
|
+
* "this is a diagram" marker that survives round-trips (the language class
|
|
46
|
+
* round-trips; fence meta does not). Detection of a tagged fence is lenient
|
|
47
|
+
* (≥1 box, no lattice rejector) so a degenerate diagram stays a diagram.
|
|
48
|
+
*/
|
|
49
|
+
export function isExplicitDiagramLang(lang: string | null | undefined): boolean {
|
|
50
|
+
return typeof lang === 'string' && lang.trim().toLowerCase() === 'diagram';
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface DetectDiagramOptions {
|
|
54
|
+
/** The fence is explicitly `diagram`-tagged: accept ≥1 box, skip rejectors. */
|
|
55
|
+
explicit?: boolean;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function detectAsciiDiagram(
|
|
59
|
+
text: string,
|
|
60
|
+
opts: DetectDiagramOptions = {},
|
|
61
|
+
): AsciiDiagramDetection {
|
|
62
|
+
const explicit = opts.explicit === true;
|
|
63
|
+
const reasons: string[] = [];
|
|
64
|
+
|
|
65
|
+
// ---- Cheap prefilter (no parse) ----------------------------------------
|
|
66
|
+
const lines = text.replace(/\r\n?/g, '\n').split('\n');
|
|
67
|
+
if (lines.length < MIN_LINES) {
|
|
68
|
+
return { isDiagram: false, reasons: [`too-few-lines(${lines.length})`] };
|
|
69
|
+
}
|
|
70
|
+
if (lines.length > MAX_LINES) {
|
|
71
|
+
return { isDiagram: false, reasons: [`too-many-lines(${lines.length})`] };
|
|
72
|
+
}
|
|
73
|
+
let maxCols = 0;
|
|
74
|
+
for (const line of lines) maxCols = Math.max(maxCols, line.length);
|
|
75
|
+
if (maxCols > MAX_COLS) {
|
|
76
|
+
return { isDiagram: false, reasons: [`too-wide(${maxCols})`] };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (!explicit) {
|
|
80
|
+
let tlCandidates = 0;
|
|
81
|
+
for (const line of lines) {
|
|
82
|
+
// Unicode TL corners, junction-started shared borders (`├──`), and
|
|
83
|
+
// ASCII `+-` starts. Junctions over-count (file trees use `├──`), but
|
|
84
|
+
// the prefilter only gates the full parse — box tracing decides.
|
|
85
|
+
const unicodeMatches = line.match(/[┌╭┏╔]|[├┬┼┣┳╋╠╦╬][─━═]/gu);
|
|
86
|
+
tlCandidates += unicodeMatches ? unicodeMatches.length : 0;
|
|
87
|
+
const plusMatches = line.match(/\+[-+]/g);
|
|
88
|
+
tlCandidates += plusMatches ? plusMatches.length : 0;
|
|
89
|
+
}
|
|
90
|
+
if (tlCandidates < 2) {
|
|
91
|
+
return { isDiagram: false, reasons: [`too-few-corner-candidates(${tlCandidates})`] };
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Markdown/GFM table shape: mostly `| … |` rows plus a separator row.
|
|
96
|
+
const nonBlank = lines.filter((l) => l.trim().length > 0);
|
|
97
|
+
const pipeRows = nonBlank.filter((l) => /^\s*\|.*\|\s*$/.test(l)).length;
|
|
98
|
+
const hasGfmSeparator = nonBlank.some((l) => /^\s*\|?[\s:|-]+\|?\s*$/.test(l) && l.includes('-'));
|
|
99
|
+
if (pipeRows > nonBlank.length / 2 && hasGfmSeparator) {
|
|
100
|
+
return { isDiagram: false, reasons: ['markdown-table'] };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// ---- Full parse + acceptance -------------------------------------------
|
|
104
|
+
const { diagram, stats } = parseAsciiDiagramWithStats(text);
|
|
105
|
+
|
|
106
|
+
const minBoxes = explicit ? 1 : 2;
|
|
107
|
+
if (diagram.nodes.length < minBoxes) {
|
|
108
|
+
return { isDiagram: false, reasons: [`too-few-boxes(${diagram.nodes.length})`] };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Garbled-label guard: a clean box label never contains box-drawing
|
|
112
|
+
// corners/junctions/verticals — those only leak in when the label columns
|
|
113
|
+
// are desynced from the box borders (hand-drawn art whose labels overflow
|
|
114
|
+
// their boxes and shove every following column out of alignment row-to-row,
|
|
115
|
+
// so no parser can map the text back to the boxes). When a meaningful share
|
|
116
|
+
// of labels are corrupt this way, the "diagram" would render as garbage, so
|
|
117
|
+
// decline detection and let the fence fall back to a faithful code block.
|
|
118
|
+
const garbled = diagram.nodes.filter((n) => STRUCTURAL_IN_LABEL.test(n.label)).length;
|
|
119
|
+
if (garbled > 0 && garbled >= diagram.nodes.length * 0.25) {
|
|
120
|
+
return { isDiagram: false, reasons: [`garbled-labels(${garbled}/${diagram.nodes.length})`] };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (!explicit) {
|
|
124
|
+
// Table-lattice rejector: an edge-less shared-border grid of ≥2×2 boxes
|
|
125
|
+
// is a psql/MySQL-style result table, not a diagram. Single-column
|
|
126
|
+
// stacks still pass (a legitimate "layer stack" diagram).
|
|
127
|
+
if (diagram.edges.length === 0 && diagram.nodes.length >= 4 && isLattice(diagram)) {
|
|
128
|
+
return { isDiagram: false, reasons: ['table-lattice'] };
|
|
129
|
+
}
|
|
130
|
+
if (stats.totalNonSpaceCells > 0) {
|
|
131
|
+
const looseRatio = stats.looseNonSpaceCells / stats.totalNonSpaceCells;
|
|
132
|
+
if (looseRatio > MAX_LOOSE_RATIO) {
|
|
133
|
+
return { isDiagram: false, reasons: [`loose-ratio(${looseRatio.toFixed(2)})`] };
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
reasons.push(`boxes(${diagram.nodes.length})`, `edges(${diagram.edges.length})`);
|
|
139
|
+
if (explicit) reasons.push('explicit');
|
|
140
|
+
return { isDiagram: true, diagram, reasons };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Convenience gate for pipeline callers: lang allowlist + content detection. */
|
|
144
|
+
export function isAsciiDiagramFence(node: MarkdownCodeBlock): boolean {
|
|
145
|
+
if (node.type !== 'code') return false;
|
|
146
|
+
if (!isEligibleAsciiFenceLang(node.lang)) return false;
|
|
147
|
+
return detectAsciiDiagram(node.value, { explicit: isExplicitDiagramLang(node.lang) }).isDiagram;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function isLattice(diagram: {
|
|
151
|
+
nodes: ReadonlyArray<{
|
|
152
|
+
col: number;
|
|
153
|
+
row: number;
|
|
154
|
+
wCols: number;
|
|
155
|
+
hRows: number;
|
|
156
|
+
containerId?: string;
|
|
157
|
+
}>;
|
|
158
|
+
}): boolean {
|
|
159
|
+
const leaves = diagram.nodes.filter((n) => !n.containerId);
|
|
160
|
+
if (leaves.length < 4) return false;
|
|
161
|
+
const rowBands = new Set(leaves.map((n) => n.row));
|
|
162
|
+
const colBands = new Set(leaves.map((n) => n.col));
|
|
163
|
+
if (rowBands.size < 2 || colBands.size < 2) return false;
|
|
164
|
+
// Every box shares a border line with some other box.
|
|
165
|
+
return leaves.every((a) =>
|
|
166
|
+
leaves.some((b) => {
|
|
167
|
+
if (a === b) return false;
|
|
168
|
+
const aR1 = a.row + a.hRows - 1;
|
|
169
|
+
const aC1 = a.col + a.wCols - 1;
|
|
170
|
+
const bR1 = b.row + b.hRows - 1;
|
|
171
|
+
const bC1 = b.col + b.wCols - 1;
|
|
172
|
+
return a.row === bR1 || aR1 === b.row || a.col === bC1 || aC1 === b.col;
|
|
173
|
+
}),
|
|
174
|
+
);
|
|
175
|
+
}
|