Skip to content

Commit c9fc6df

Browse files
committed
refactor: extract paragraph helpers
1 parent 4ae5421 commit c9fc6df

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

src/typstToTextlintAst.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -924,23 +924,24 @@ export const paragraphizeTextlintAstObject = (
924924
return { nodes: collected, nextIndex: i };
925925
};
926926

927+
const isBlankStr = (c: Content): boolean =>
928+
c.type === ASTNodeTypes.Str && c.raw?.trim() === "";
929+
const isTermItem = (c: Content): boolean =>
930+
isAstNode(c) && isTypstType(c.type, /^Marked::TermItem$/);
931+
const createParagraph = (nodes: Content[]): Content => {
932+
const first = nodes[0];
933+
const last = nodes[nodes.length - 1];
934+
return {
935+
type: ASTNodeTypes.Paragraph,
936+
children: nodes,
937+
loc: { start: first.loc.start, end: last.loc.end },
938+
range: [first.range[0], last.range[1]],
939+
raw: nodes.map((c) => c.raw).join(""),
940+
} as Content;
941+
};
942+
927943
const splitParagraphTermlist = (paraNode: AstNode): Content[] => {
928944
if (!hasChildren(paraNode)) return [];
929-
const isBlankStr = (c: Content): boolean =>
930-
c.type === ASTNodeTypes.Str && c.raw?.trim() === "";
931-
const isTermItem = (c: Content): boolean =>
932-
isAstNode(c) && isTypstType(c.type, /^Marked::TermItem$/);
933-
const createParagraph = (nodes: Content[]): Content => {
934-
const first = nodes[0];
935-
const last = nodes[nodes.length - 1];
936-
return {
937-
type: ASTNodeTypes.Paragraph,
938-
children: nodes,
939-
loc: { start: first.loc.start, end: last.loc.end },
940-
range: [first.range[0], last.range[1]],
941-
raw: nodes.map((c) => c.raw).join(""),
942-
} as Content;
943-
};
944945

945946
const segments: { isTerm: boolean; nodes: Content[] }[] = [];
946947
let current: { isTerm: boolean; nodes: Content[] } | null = null;
@@ -975,16 +976,16 @@ export const paragraphizeTextlintAstObject = (
975976
const collectConsecutiveRootTermItems = (
976977
arr: Content[],
977978
startIndex: number,
978-
): { termItems: Content[]; nextIndex: number } => {
979-
const termItems: Content[] = [];
979+
): { nodes: Content[]; nextIndex: number } => {
980+
const nodes: Content[] = [];
980981
let i = startIndex;
981982
while (i < arr.length) {
982983
const currentNode = arr[i];
983984
if (
984985
isAstNode(currentNode) &&
985986
isTypstType(currentNode.type, /^Marked::TermItem$/)
986987
) {
987-
termItems.push(currentNode);
988+
nodes.push(currentNode);
988989
i++;
989990
continue;
990991
}
@@ -1000,7 +1001,7 @@ export const paragraphizeTextlintAstObject = (
10001001
}
10011002
break;
10021003
}
1003-
return { termItems, nextIndex: i };
1004+
return { nodes, nextIndex: i };
10041005
};
10051006
const sourceChildren = rootNode.children;
10061007

@@ -1018,11 +1019,11 @@ export const paragraphizeTextlintAstObject = (
10181019
const node = sourceChildren[i];
10191020

10201021
if (isAstNode(node) && isTypstType(node.type, /^Marked::TermItem$/)) {
1021-
const { termItems, nextIndex } = collectConsecutiveRootTermItems(
1022+
const { nodes, nextIndex } = collectConsecutiveRootTermItems(
10221023
sourceChildren,
10231024
i,
10241025
);
1025-
children.push(...termItems);
1026+
children.push(...nodes);
10261027
i = nextIndex;
10271028
continue;
10281029
}

0 commit comments

Comments
 (0)