Skip to content

Commit 3f78539

Browse files
committed
Fixed flag extraction in parsing
1 parent 83698fb commit 3f78539

3 files changed

Lines changed: 96 additions & 101 deletions

File tree

‎main.js‎

Lines changed: 44 additions & 44 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/Parsing.ts‎

Lines changed: 13 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,10 @@ export function getAllParameters(originalLineText: string, settings: CodeblockCu
212212
const headerDisplayText = extractFileTitle(parsedParameters);
213213
const hasTitle = !!headerDisplayText;
214214

215-
// fold
216-
const fold = isFoldDefined(lineText);
217-
218-
// unfold
219-
const unfold = isUnFoldDefined(lineText);
215+
// flags (fold, unfold, exclude, noprompt, noparse, output, expand)
216+
const flags = getFlagWords(lineText);
217+
const fold = flags.has("fold");
218+
const unfold = flags.has("unfold");
220219

221220
// language
222221
const language = getCodeBlockLanguage(lineText, isReadingView);
@@ -225,7 +224,7 @@ export function getAllParameters(originalLineText: string, settings: CodeblockCu
225224
const displayLanguage = getDisplayLanguageName(language) || "Plain text";
226225

227226
// isExcluded
228-
const exclude = isExcluded(lineText, settings.ExcludeLangs);
227+
const exclude = flags.has("exclude") || isExcluded(lineText, settings.ExcludeLangs);
229228

230229
// group
231230
const group = extractParameter(parsedParameters, "group") ?? '';
@@ -253,11 +252,11 @@ export function getAllParameters(originalLineText: string, settings: CodeblockCu
253252
let parsePromptId = extractParameter(parsedParameters, "parse");
254253

255254
// noprompt
256-
const noPrompt = isParameterDefined("noprompt", lineText);
255+
const noPrompt = flags.has("noprompt");
257256
const noPromptLines = getLineRanges(extractParameter(parsedParameters, "noprompt"));
258257

259258
// noparse
260-
const noParse = isParameterDefined("noparse", lineText);
259+
const noParse = flags.has("noparse");
261260
const noParseLines = getLineRanges(extractParameter(parsedParameters, "noparse"));
262261

263262
if (!parsePromptId && language && !noParse) {
@@ -273,13 +272,13 @@ export function getAllParameters(originalLineText: string, settings: CodeblockCu
273272
}
274273

275274
// output
276-
const output = isParameterDefined("output", lineText);
275+
const output = flags.has("output");
277276

278277
// hide
279278
const hideLines = getLineRanges(extractParameter(parsedParameters, "hide"));
280279

281280
// expand
282-
const expand = isParameterDefined("expand", lineText);
281+
const expand = flags.has("expand");
283282

284283
return {
285284
defaultLinesToHighlight: defaultLinesToHighlight,
@@ -965,50 +964,10 @@ function determineLineNumberDisplay(parsedParameters: ParsedParams) {
965964
return { isSpecificNumber, showNumbers, lineNumberOffset, lineNumberJumps };
966965
}// determineLineNumberDisplay
967966

968-
function isFoldDefined(str: string): boolean {
969-
return isParameterDefined("fold", str);
970-
}// isFoldDefined
971-
972-
function isUnFoldDefined(str: string): boolean {
973-
return isParameterDefined("unfold", str);
974-
}// isUnFoldDefined
975-
976-
const isParamDefinedCache = new Map<string, { mid: RegExp, end: RegExp }>();
977-
function getParamDefinedRegexes(term: string): { mid: RegExp, end: RegExp } {
978-
let cached = isParamDefinedCache.get(term);
979-
if (!cached) {
980-
cached = {
981-
mid: new RegExp(`^(?:\`|~){3,}\\w*\\s*${term}\\s`),
982-
end: new RegExp(`^(?:\`|~){3,}\\w*\\s*${term}$`)
983-
};
984-
isParamDefinedCache.set(term, cached);
985-
}
986-
return cached;
987-
}// getParamDefinedRegexes
988-
989-
function isParameterDefined(searchTerm: string, str: string): boolean {
990-
str = str.toLowerCase();
991-
searchTerm = searchTerm.toLowerCase();
992-
993-
if (str.includes(` ${searchTerm} `)) {
994-
return true;
995-
}
996-
997-
// check if parameter is at end of string with space before it
998-
if (str.endsWith(' ' + searchTerm)) {
999-
return true;
1000-
}
1001-
1002-
const { mid: fenceAndLangRegex, end: fenceAndLangEndRegex } = getParamDefinedRegexes(searchTerm);
1003-
if (fenceAndLangRegex.test(str)) {
1004-
return true;
1005-
}
1006-
1007-
if (fenceAndLangEndRegex.test(str)) {
1008-
return true;
1009-
}
1010-
return false;
1011-
}// isParameterDefined
967+
function getFlagWords(lineText: string): Set<string> {
968+
const withoutFence = lineText.replace(/^[\s>]*(?:`|~)*/, '');
969+
return new Set(withoutFence.replace(PARAM_REGEX_PATTERN, ' ').toLowerCase().split(/\s+/).filter(Boolean));
970+
}// getFlagWords
1012971

1013972
function getCodeBlockLanguage(str: string, isReadingView = false): string {
1014973
const originalStr = str;
@@ -1057,9 +1016,6 @@ let excludeRegexCacheKey = '';
10571016
let excludeRegexCacheValue: RegExp[] = [];
10581017

10591018
function isExcluded(lineText: string, excludeLangs: string): boolean {
1060-
if (isParameterDefined("exclude", lineText))
1061-
return true;
1062-
10631019
const codeBlockLang = getCodeBlockLanguage(lineText);
10641020
let regexLangs: RegExp[];
10651021
if (excludeLangs === excludeRegexCacheKey) {

‎tests/parsing.test.cjs‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,45 @@ test('fold, unfold, expand, output flags', async () => {
108108
assert.equal((await parse('```cpp folded')).fold, false);
109109
});
110110

111+
test('flags count only as whole words outside key:value pairs', async () => {
112+
const flags = p => [p.fold, p.unfold, p.exclude, p.output, p.expand, p.noPrompt, p.noParse];
113+
const none = [false, false, false, false, false, false, false];
114+
115+
// words inside a quoted value are not flags
116+
assert.deepEqual(flags(await parse('```cpp title:"how to fold paper"')), none);
117+
assert.deepEqual(flags(await parse('```cpp title:"do not exclude me"')), none);
118+
assert.deepEqual(flags(await parse('```cpp title:"fold unfold exclude output expand noprompt noparse"')), none);
119+
assert.deepEqual(flags(await parse("```cpp hlt:'fold' file:\"x expand\"")), none);
120+
121+
// a flag is not part of a longer word (```unfold is not fold, ```manifold is not fold)
122+
let p = await parse('```unfold');
123+
assert.deepEqual([p.fold, p.unfold], [false, true]);
124+
p = await parse('```cpp unfold');
125+
assert.deepEqual([p.fold, p.unfold], [false, true]);
126+
assert.equal((await parse('```manifold')).fold, false);
127+
assert.equal((await parse('```cpp noexclude')).exclude, false);
128+
129+
// still found after a quoted value, between parameters, at the start and the end, with tabs, with ~~~
130+
assert.equal((await parse('```cpp title:"how to fold paper" fold')).fold, true);
131+
assert.equal((await parse('```cpp hl:1 fold title:x')).fold, true);
132+
assert.equal((await parse('```exclude')).exclude, true);
133+
assert.equal((await parse('~~~cpp\texpand')).expand, true);
134+
assert.equal((await parse('````cpp output')).output, true);
135+
p = await parse('```bash noprompt noparse:2');
136+
assert.deepEqual([p.noPrompt, p.noParse, p.noParseLines], [true, false, [2]]);
137+
138+
// a parameter with a value is not a flag
139+
assert.deepEqual([(await parse('```bash noprompt:1-3')).noPrompt, (await parse('```bash noparse:1')).noParse], [false, false]);
140+
141+
// an unquoted value ends at a space, so the rest are words (documented: titles with spaces need quotes)
142+
assert.equal((await parse('```cpp title:how to fold')).fold, true);
143+
144+
// works when the line does not start with the fence (code block inside a quote)
145+
assert.equal((await parse('> ```cpp fold')).fold, true);
146+
assert.equal((await parse('> > ```fold')).fold, true);
147+
assert.equal((await parse('> ```cpp title:"fold"')).fold, false);
148+
});
149+
111150
test('exclude flag and excluded languages with wildcards', async () => {
112151
assert.equal((await parse('```cpp exclude')).exclude, true);
113152
assert.equal((await parse('```cpp')).exclude, false);

0 commit comments

Comments
 (0)