Skip to content

Commit

Permalink
factor out notRegexpBefore
Browse files Browse the repository at this point in the history
  • Loading branch information
lily committed Sep 9, 2024
1 parent 9cd9aae commit 27bba7e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
9 changes: 9 additions & 0 deletions src/internal/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,15 @@ export function notMatch(parser: Parser<unknown>): Parser<null> {
});
}

export function notRegexpBefore<T extends RegExp>(pattern: T): Parser<null> {
return new Parser((input, index, state) => {
const beforeStr = input.slice(0, index);
return !pattern.test(beforeStr)
? success(index, null)
: failure();
});
}

export const cr = str('\r');
export const lf = str('\n');
export const crlf = str('\r\n');
Expand Down
34 changes: 9 additions & 25 deletions src/internal/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ export const language = P.createLanguage<TypeTable>({
italicAsta: () => {
const mark = P.str('*');
const parser = P.seq(
P.notRegexpBefore(/[a-z0-9]$/i),
mark,
P.alt([alphaAndNum, space]).many(1),
mark,
Expand All @@ -401,18 +402,14 @@ export const language = P.createLanguage<TypeTable>({
if (!result.success) {
return P.failure();
}
// check before
const beforeStr = input.slice(0, index);
if (/[a-z0-9]$/i.test(beforeStr)) {
return P.failure();
}
return P.success(result.index, M.ITALIC(mergeText(result.value[1])));
return P.success(result.index, M.ITALIC(mergeText(result.value[2])));
});
},

italicUnder: () => {
const mark = P.str('_');
const parser = P.seq(
P.notRegexpBefore(/[a-z0-9]$/i),
mark,
P.alt([alphaAndNum, space]).many(1),
mark,
Expand All @@ -422,12 +419,7 @@ export const language = P.createLanguage<TypeTable>({
if (!result.success) {
return P.failure();
}
// check before
const beforeStr = input.slice(0, index);
if (/[a-z0-9]$/i.test(beforeStr)) {
return P.failure();
}
return P.success(result.index, M.ITALIC(mergeText(result.value[1])));
return P.success(result.index, M.ITALIC(mergeText(result.value[2])));
});
},

Expand Down Expand Up @@ -551,6 +543,7 @@ export const language = P.createLanguage<TypeTable>({
mention: () => {
const parser = P.seq(
notLinkLabel,
P.notRegexpBefore(/[a-z0-9]$/i),
P.str('@'),
P.regexp(/[a-z0-9_-]+/i),
P.seq(
Expand All @@ -564,15 +557,10 @@ export const language = P.createLanguage<TypeTable>({
if (!result.success) {
return P.failure();
}
// check before (not mention)
const beforeStr = input.slice(0, index);
if (/[a-z0-9]$/i.test(beforeStr)) {
return P.failure();
}
let invalidMention = false;
const resultIndex = result.index;
const username: string = result.value[2];
const hostname: string | null = result.value[3];
const username: string = result.value[3];
const hostname: string | null = result.value[4];
// remove [.-] of tail of hostname
let modifiedHost = hostname;
if (hostname != null) {
Expand Down Expand Up @@ -637,19 +625,15 @@ export const language = P.createLanguage<TypeTable>({
]));
const parser = P.seq(
notLinkLabel,
P.notRegexpBefore(/[a-z0-9]$/i),
mark,
innerItem.many(1).text(),
).select(2);
).select(3);
return new P.Parser((input, index, state) => {
const result = parser.handler(input, index, state);
if (!result.success) {
return P.failure();
}
// check before
const beforeStr = input.slice(0, index);
if (/[a-z0-9]$/i.test(beforeStr)) {
return P.failure();
}
const resultIndex = result.index;
const resultValue = result.value;
// disallow number only
Expand Down

0 comments on commit 27bba7e

Please sign in to comment.