Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Cards missing when horizontal rule present in document #970

Merged
merged 7 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 33 additions & 35 deletions src/util/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import moment from "moment";
import { Moment } from "moment";
import { normalize, sep } from "path";
import { PREFERRED_DATE_FORMAT, YAML_FRONT_MATTER_REGEX } from "src/constants";

Check warning on line 4 in src/util/utils.ts

View workflow job for this annotation

GitHub Actions / lint_and_test

'YAML_FRONT_MATTER_REGEX' is defined but never used. Allowed unused vars must match /^_/u

type Hex = number;

Expand Down Expand Up @@ -129,45 +129,43 @@
return true;
}

//
// This returns [frontmatter, content]
//
// The returned content has the same number of lines as the supplied str string, but with the
// frontmatter lines (if present) blanked out.
//
// 1. We don't want the parser to see the frontmatter, as it would deem it to be part of a multi-line question
// if one started on the line immediately after the "---" closing marker.
//
// 2. The lines are blanked out rather than deleted so that line numbers are not affected
// e.g. for calls to getQuestionContext(cardLine: number)
//
/**
* The returned content has the same number of lines as the supplied string, but with the frontmatter lines (if present) blanked out.
*
* 1. We don't want the parser to see the frontmatter, as it would deem it to be part of a multi-line question if one started on the line immediately after the "---" closing marker.
*
* 2. The lines are blanked out rather than deleted so that line numbers are not affected e.g. for calls to getQuestionContext(cardLine: number)
*
* @param str The file content as string
* @returns [frontmatter, content]
*/
export function extractFrontmatter(str: string): [string, string] {
let frontmatter: string = "";
let content: string = "";
let frontmatterEndLineNum: number = null;
if (YAML_FRONT_MATTER_REGEX.test) {
const lines: string[] = splitTextIntoLineArray(str);

// The end "---" marker must be on the third line (index 2) or later
for (let i = 2; i < lines.length; i++) {
if (lines[i] == "---") {
frontmatterEndLineNum = i;
break;
}
const lines = splitTextIntoLineArray(str);
let lineIndex = 0;
let hasFrontmatter = false;
do {
// Starts file with '---'
if (lineIndex === 0 && lines[lineIndex] === "---") {
hasFrontmatter = true;
}

if (frontmatterEndLineNum) {
const frontmatterStartLineNum: number = 0;
const frontmatterLines: string[] = [];
for (let i = frontmatterStartLineNum; i <= frontmatterEndLineNum; i++) {
frontmatterLines.push(lines[i]);
lines[i] = "";
}
frontmatter = frontmatterLines.join("\n");
content = lines.join("\n");
// Line is end of front matter
else if (hasFrontmatter && lines[lineIndex] === "---") {
hasFrontmatter = false;
lineIndex++;
}
if (hasFrontmatter) {
lineIndex++;
}
} while (hasFrontmatter && lineIndex < lines.length);
// No end of Frontmatter found
if (hasFrontmatter) {
lineIndex = 0;
}
if (frontmatter.length == 0) content = str;

const frontmatter: string = lines.slice(0, lineIndex).join("\n");
const emptyLines: string[] = lineIndex > 0 ? Array(lineIndex).join(".").split(".") : [];
const content: string = emptyLines.concat(lines.slice(lineIndex)).join("\n");

return [frontmatter, content];
}

Expand Down
170 changes: 170 additions & 0 deletions tests/unit/util/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,176 @@ ${content}`;
${content}`;
expect(c).toEqual(expectedContent);
});

test("With frontmatter and content (Horizontal line)", () => {
const frontmatter: string = `---
sr-due: 2024-01-17
sr-interval: 16
sr-ease: 278
tags:
- flashcards/aws
- flashcards/datascience
---`;
const frontmatterBlankedOut: string = `






`;
const content: string = `#flashcards/science/chemistry


---
# Questions
---


Chemistry Question from file underelephant 4A::goodby

<!--SR:!2023-11-02,17,290-->

Chemistry Question from file underdog 4B::goodby

<!--SR:!2023-12-18,57,310-->

---

Chemistry Question from file underdog 4C::goodby

<!--SR:!2023-10-25,3,210-->

This single {{question}} turns into {{3 separate}} {{cards}}

<!--SR:!2023-10-20,1,241!2023-10-25,3,254!2023-10-23,1,221-->

---`;

const text: string = `${frontmatter}
${content}`;
const expectedContent: string = `${frontmatterBlankedOut}
${content}`;

const [f, c] = extractFrontmatter(text);
expect(f).toEqual(frontmatter);
expect(c).toEqual(expectedContent);
});

test("With frontmatter and content (Horizontal line newLine)", () => {
const frontmatter: string = `---
sr-due: 2024-01-17
sr-interval: 16
sr-ease: 278
tags:
- flashcards/aws
- flashcards/datascience
---`;
const frontmatterBlankedOut: string = `






`;
const content: string = `#flashcards/science/chemistry


---
# Questions
---


Chemistry Question from file underelephant 4A::goodby

<!--SR:!2023-11-02,17,290-->

Chemistry Question from file underdog 4B::goodby

<!--SR:!2023-12-18,57,310-->

---

Chemistry Question from file underdog 4C::goodby

<!--SR:!2023-10-25,3,210-->

This single {{question}} turns into {{3 separate}} {{cards}}

<!--SR:!2023-10-20,1,241!2023-10-25,3,254!2023-10-23,1,221-->

---
`;

const text: string = `${frontmatter}
${content}`;
const expectedContent: string = `${frontmatterBlankedOut}
${content}`;

const [f, c] = extractFrontmatter(text);
expect(f).toEqual(frontmatter);
expect(c).toEqual(expectedContent);
});

test("With frontmatter and content (Horizontal line codeblock)", () => {
const frontmatter: string = `---
sr-due: 2024-01-17
sr-interval: 16
sr-ease: 278
tags:
- flashcards/aws
- flashcards/datascience
---`;
const frontmatterBlankedOut: string = `






`;
const content: string = [
"```",
"---",
"```",
"#flashcards/science/chemistry",
"# Questions",
" ",
"",
"Chemistry Question from file underelephant 4A::goodby",
"",
"<!--SR:!2023-11-02,17,290-->",
"",
"Chemistry Question from file underdog 4B::goodby",
"",
"<!--SR:!2023-12-18,57,310-->",
"```",
"---",
"```",
"",
"Chemistry Question from file underdog 4C::goodby",
"",
"<!--SR:!2023-10-25,3,210-->",
"",
"This single {{question}} turns into {{3 separate}} {{cards}}",
"",
"<!--SR:!2023-10-20,1,241!2023-10-25,3,254!2023-10-23,1,221-->",
"",
"```",
"---",
"```",
].join("\n");

const text: string = `${frontmatter}
${content}`;
const expectedContent: string = `${frontmatterBlankedOut}
${content}`;

const [f, c] = extractFrontmatter(text);
expect(f).toEqual(frontmatter);
expect(c).toEqual(expectedContent);
});
});

describe("findLineIndexOfSearchStringIgnoringWs", () => {
Expand Down
Loading