-
Notifications
You must be signed in to change notification settings - Fork 268
Expand file tree
/
Copy pathparser.ts
More file actions
207 lines (176 loc) · 6.86 KB
/
parser.ts
File metadata and controls
207 lines (176 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { ClozeCrafter } from "clozecraft";
import { CardType } from "src/card/questions/question";
export let debugParser = false;
export interface ParserOptions {
singleLineCardSeparator: string;
singleLineReversedCardSeparator: string;
multilineCardSeparator: string;
multilineReversedCardSeparator: string;
multilineCardEndMarker: string;
clozePatterns: string[];
}
export function setDebugParser(value: boolean) {
debugParser = value;
}
export class ParsedQuestionInfo {
cardType: CardType;
text: string;
// Line numbers start at 0
firstLineNum: number;
lastLineNum: number;
constructor(cardType: CardType, text: string, firstLineNum: number, lastLineNum: number) {
this.cardType = cardType;
this.text = text;
this.firstLineNum = firstLineNum;
this.lastLineNum = lastLineNum;
}
isQuestionLineNum(lineNum: number): boolean {
return lineNum >= this.firstLineNum && lineNum <= this.lastLineNum;
}
}
function markerInsideCodeBlock(text: string, marker: string, markerIndex: number): boolean {
let goingBack = markerIndex - 1,
goingForward = markerIndex + marker.length;
let backTicksBefore = 0,
backTicksAfter = 0;
while (goingBack >= 0) {
if (text[goingBack] === "`") backTicksBefore++;
goingBack--;
}
while (goingForward < text.length) {
if (text[goingForward] === "`") backTicksAfter++;
goingForward++;
}
// If there's an odd number of backticks before and after,
// the marker is inside an inline code block
return backTicksBefore % 2 === 1 && backTicksAfter % 2 === 1;
}
function hasInlineMarker(text: string, marker: string): boolean {
// No marker provided
if (marker.length === 0) return false;
// Check if the marker is in the text
const markerIdx = text.indexOf(marker);
if (markerIdx === -1) return false;
// Check if it's inside an inline code block
return !markerInsideCodeBlock(text, marker, markerIdx);
}
/**
* Returns flashcards found in `text`
*
* It is best that the text does not contain frontmatter, see extractFrontmatter for reasoning
*
* @param text - The text to extract flashcards from
* @param ParserOptions - Parser options
* @returns An array of parsed question information
*/
export function parse(text: string, options: ParserOptions): ParsedQuestionInfo[] {
if (debugParser) {
console.log("Text to parse:\n<<<" + text + ">>>");
}
// Sort inline separators by length, longest first
const inlineSeparators = [
{ separator: options.singleLineCardSeparator, type: CardType.SingleLineBasic },
{ separator: options.singleLineReversedCardSeparator, type: CardType.SingleLineReversed },
];
inlineSeparators.sort((a, b) => b.separator.length - a.separator.length);
const cards: ParsedQuestionInfo[] = [];
let cardText = "";
let cardType: CardType | null = null;
let firstLineNo = 0,
lastLineNo: number;
const clozecrafter = new ClozeCrafter(options.clozePatterns);
const lines: string[] = text.replaceAll("\r\n", "\n").split("\n");
for (let i = 0; i < lines.length; i++) {
const currentLine = lines[i],
currentTrimmed = lines[i].trim();
// Skip everything in HTML comments
if (currentLine.startsWith("<!--") && !currentLine.startsWith("<!--SR:")) {
while (i + 1 < lines.length && !currentLine.includes("-->")) i++;
i++;
continue;
}
// Have we reached the end of a card?
const isEmptyLine = currentTrimmed.length === 0;
const hasMultilineCardEndMarker =
options.multilineCardEndMarker && currentTrimmed === options.multilineCardEndMarker;
if (
// We've probably reached the end of a card
(isEmptyLine && !options.multilineCardEndMarker) ||
// Empty line & we're not picking up any card
(isEmptyLine && cardType === null) ||
// We've reached the end of a multi line card &
// we're using custom end markers
hasMultilineCardEndMarker
) {
if (cardType) {
// Create a new card
lastLineNo = i - 1;
cards.push(
new ParsedQuestionInfo(cardType, cardText.trimEnd(), firstLineNo, lastLineNo),
);
cardType = null;
}
cardText = "";
firstLineNo = i + 1;
continue;
}
// Update card text
if (cardText.length > 0) {
cardText += "\n";
}
cardText += currentLine.trimEnd();
// Pick up inline cards
for (const { separator, type } of inlineSeparators) {
if (hasInlineMarker(currentLine, separator)) {
cardType = type;
break;
}
}
if (cardType === CardType.SingleLineBasic || cardType === CardType.SingleLineReversed) {
cardText = currentLine;
firstLineNo = i;
// Pick up scheduling information if present
if (i + 1 < lines.length && lines[i + 1].startsWith("<!--SR:")) {
cardText += "\n" + lines[i + 1];
i++;
}
lastLineNo = i;
cards.push(new ParsedQuestionInfo(cardType, cardText, firstLineNo, lastLineNo));
cardType = null;
cardText = "";
} else if (currentTrimmed === options.multilineCardSeparator) {
// Ignore card if the front of the card is empty
if (cardText.length > 1) {
// Pick up multiline basic cards
cardType = CardType.MultiLineBasic;
}
} else if (currentTrimmed === options.multilineReversedCardSeparator) {
// Ignore card if the front of the card is empty
if (cardText.length > 1) {
// Pick up multiline basic cards
cardType = CardType.MultiLineReversed;
}
} else if (currentLine.startsWith("```") || currentLine.startsWith("~~~")) {
// Pick up codeblocks
const codeBlockClose = currentLine.match(/`+|~+/)[0];
while (i + 1 < lines.length && !lines[i + 1].startsWith(codeBlockClose)) {
i++;
cardText += "\n" + lines[i];
}
cardText += "\n" + codeBlockClose;
i++;
} else if (cardType === null && clozecrafter.isClozeNote(currentLine)) {
// Pick up cloze cards
cardType = CardType.Cloze;
}
}
// Do we have a card left in the queue?
if (cardType && cardText) {
lastLineNo = lines.length - 1;
cards.push(new ParsedQuestionInfo(cardType, cardText.trimEnd(), firstLineNo, lastLineNo));
}
if (debugParser) {
console.log("Parsed cards:\n", cards);
}
return cards;
}