Skip to content

Commit 73e2727

Browse files
committed
fix(zalouser): preserve nested container semantics
1 parent fa3d5e2 commit 73e2727

5 files changed

Lines changed: 87 additions & 5 deletions

File tree

‎extensions/zalouser/src/text-styles-blocks.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,21 @@ describe("parseZalouserTextStyles blocks", () => {
231231
text: "quoted",
232232
styles: [{ start: 0, len: 6, st: TextStyle.Indent, indentSize: 2 }],
233233
});
234+
expect(parseZalouserTextStyles("> > nested")).toEqual({
235+
text: "nested",
236+
styles: [{ start: 0, len: 6, st: TextStyle.Indent, indentSize: 2 }],
237+
});
238+
});
239+
240+
it("keeps blockquote padding structural before headings", () => {
241+
expect(parseZalouserTextStyles("> # Title")).toEqual({
242+
text: "Title",
243+
styles: [
244+
{ start: 0, len: 5, st: TextStyle.Bold },
245+
{ start: 0, len: 5, st: TextStyle.Big },
246+
{ start: 0, len: 5, st: TextStyle.Indent, indentSize: 1 },
247+
],
248+
});
234249
});
235250

236251
it("keeps container-only nested blockquotes empty", () => {

‎extensions/zalouser/src/text-styles-compile.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
import {
1010
sourceContainerPrefixLength,
1111
sourceContainerProjection,
12+
sourceListItemContent,
1213
} from "./text-styles-source-spans.js";
1314
import { sourceAtxIsMarkerOnly } from "./text-styles-source.js";
1415
import { TextStyle } from "./zca-constants.js";
@@ -50,10 +51,8 @@ export function collectBlockEdits(
5051
block.end <= item.end,
5152
);
5253
const sourceItem = sourceIR.listItems?.[itemIndex];
53-
const sourceContent = sourceItem?.sourceContent
54-
? source
55-
.slice(sourceItem.sourceContent.start, sourceItem.sourceContent.end)
56-
.replace(/[ \t\r\n]+$/gu, "")
54+
const sourceContent = sourceItem
55+
? sourceListItemContent(source, sourceIR, sourceLineStarts, sourceLines, sourceItem)
5756
: "";
5857
const hasRenderedContent =
5958
item.contentStart !== undefined &&

‎extensions/zalouser/src/text-styles-containers.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ describe("parseZalouserTextStyles containers", () => {
234234
text: "\u00A0",
235235
styles: [{ start: 0, len: 1, st: TextStyle.UnorderedList }],
236236
});
237+
expect(parseZalouserTextStyles("-\n \u00A0")).toEqual({
238+
text: "\u00A0",
239+
styles: [{ start: 0, len: 1, st: TextStyle.UnorderedList }],
240+
});
237241
expect(parseZalouserTextStyles("- parent\n - \u00A0")).toEqual({
238242
text: "parent\n\u00A0",
239243
styles: [

‎extensions/zalouser/src/text-styles-source-spans.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,56 @@ export function sourceContainerProjection(
9898
};
9999
}
100100

101+
export function sourceListItemContent(
102+
source: string,
103+
ir: MarkdownIRWithBlockMetadata,
104+
sourceLineStarts: number[],
105+
sourceLines: string[],
106+
item: NonNullable<MarkdownIRWithBlockMetadata["listItems"]>[number],
107+
): string {
108+
if (
109+
item.sourceContent === undefined ||
110+
item.sourceStartLine === undefined ||
111+
item.sourceEndLine === undefined
112+
) {
113+
return "";
114+
}
115+
const blockquoteDepth = (ir.blocks ?? [])
116+
.filter(
117+
(block) =>
118+
block.kind === "blockquote" &&
119+
(block.sourceStartLine ?? 0) <= item.sourceStartLine! &&
120+
(block.sourceEndLine ?? 0) >= item.sourceEndLine!,
121+
)
122+
.reduce((depth, block) => Math.max(depth, block.blockquoteDepth ?? 0), 0);
123+
const contentLines: string[] = [];
124+
for (let lineIndex = item.sourceStartLine; lineIndex < item.sourceEndLine; lineIndex += 1) {
125+
const lineStart = sourceLineStarts[lineIndex] ?? 0;
126+
const lineEnd =
127+
lineIndex + 1 < sourceLineStarts.length
128+
? Math.max(lineStart, (sourceLineStarts[lineIndex + 1] ?? source.length) - 1)
129+
: source.length;
130+
const contentStart = Math.max(item.sourceContent.start, lineStart);
131+
const contentEnd = Math.min(item.sourceContent.end, lineEnd);
132+
const projectedStart = Math.max(
133+
contentStart,
134+
lineStart +
135+
sourceContainerPrefixLength(
136+
sourceLines[lineIndex] ?? "",
137+
lineIndex,
138+
ir,
139+
sourceLineStarts,
140+
sourceLines,
141+
blockquoteDepth,
142+
),
143+
);
144+
contentLines.push(
145+
source.slice(Math.min(projectedStart, contentEnd), contentEnd).replace(/\r$/u, ""),
146+
);
147+
}
148+
return contentLines.join("\n").replace(/^\n+|[ \t\r\n]+$/gu, "");
149+
}
150+
101151
export function sourceBlockquotePrefixLength(
102152
line: string,
103153
maxDepth = Number.POSITIVE_INFINITY,

‎extensions/zalouser/src/text-styles-source.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,16 @@ export function protectInlineSyntaxOutsideCode(
110110
const emptyAtxMarkers = new Map<number, string>();
111111
const closingAtxLines = new Set<number>();
112112
const atxHeadingLines = new Set<number>();
113+
const blockquoteDepthByLine = new Map<number, number>();
113114
for (const block of ir.blocks ?? []) {
115+
if (block.kind === "blockquote") {
116+
for (let line = block.sourceStartLine ?? 0; line < (block.sourceEndLine ?? 0); line += 1) {
117+
blockquoteDepthByLine.set(
118+
line,
119+
Math.max(blockquoteDepthByLine.get(line) ?? 0, block.blockquoteDepth ?? 0),
120+
);
121+
}
122+
}
114123
if (block.kind === "heading" && block.headingOrigin === "atx") {
115124
atxHeadingLines.add(block.sourceStartLine ?? 0);
116125
}
@@ -235,6 +244,7 @@ export function protectInlineSyntaxOutsideCode(
235244
registry,
236245
atxHeadingLines.has(lineIndex),
237246
!listLines.has(lineIndex),
247+
blockquoteDepthByLine.get(lineIndex) ?? 0,
238248
);
239249
return thematicBreakLines.has(lineIndex)
240250
? line
@@ -248,9 +258,11 @@ function protectResidualBlockPadding(
248258
registry: TokenRegistry,
249259
atxHeading: boolean,
250260
protectQuotePadding: boolean,
261+
blockquoteDepth: number,
251262
): string {
252263
let protectedLine = line;
253264
let cursor = 0;
265+
let consumedBlockquotes = 0;
254266
if (protectQuotePadding) {
255267
while (cursor < protectedLine.length) {
256268
let marker = cursor;
@@ -264,10 +276,12 @@ function protectResidualBlockPadding(
264276
if (protectedLine[marker] !== ">") {
265277
break;
266278
}
279+
consumedBlockquotes += 1;
267280
const whitespaceStart = marker + 1;
268281
const whitespace = /^[ \t]+/u.exec(protectedLine.slice(whitespaceStart))?.[0] ?? "";
269282
const remainingContent = protectedLine.slice(whitespaceStart + whitespace.length);
270-
if (whitespace.length > 1 && remainingContent) {
283+
const structuralPadding = atxHeading || consumedBlockquotes < blockquoteDepth;
284+
if (!structuralPadding && whitespace.length > 1 && remainingContent) {
271285
protectedLine = `${protectedLine.slice(0, whitespaceStart + 1)}${protectLiteral(
272286
registry,
273287
whitespace.slice(1),

0 commit comments

Comments
 (0)