Skip to content

Commit 4e5b853

Browse files
committed
improve word parsing
1 parent 36b790b commit 4e5b853

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

dictionary/parser.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import nlp from "compromise/three";
55
import { nullableAsArray, throwError } from "../misc/misc.ts";
66
import {
77
all,
8-
allAtLeastOnceWithCheck,
98
allWithCheck,
109
checkedAsWhole,
1110
CheckedParser,
@@ -73,18 +72,21 @@ const keyword = memoize(<T extends string>(keyword: T) =>
7372
)
7473
) as Parser<T>
7574
);
76-
const unescapedWord = allAtLeastOnceWithCheck(
77-
new CheckedParser(
78-
choiceOnlyOne(wordCharacter, backtick),
79-
choiceWithCheck(
80-
checkedAsWhole(wordCharacter),
81-
checkedSequence(backtick, character.skip(backtick))
82-
.map(([_, character]) => character),
83-
comment.map(() => ""),
75+
const checkedCharacter = checkedAsWhole(wordCharacter);
76+
const escape = checkedSequence(backtick, character.skip(backtick))
77+
.map(([_, character]) => character);
78+
const unescapedWord = sequence(
79+
choiceWithCheck(checkedCharacter, escape),
80+
allWithCheck(
81+
new CheckedParser(
82+
choiceOnlyOne(wordCharacter, backtick, comment.check),
83+
choiceWithCheck(checkedCharacter, escape, comment.map(() => "")),
8484
),
8585
),
8686
)
87-
.map((word) => word.join("").replaceAll(/\s+/g, " ").trim());
87+
.map(([first, rest]) =>
88+
`${first}${rest.join("")}`.replaceAll(/\s+/g, " ").trim()
89+
);
8890
const word = unescapedWord.map(escapeHtml);
8991
const number = choiceOnlyOne(keyword("singular"), keyword("plural"));
9092
const optionalNumber = optionalAll(number);

0 commit comments

Comments
 (0)