From 4e80ed50b614b9663a15f09aff91982de952819a Mon Sep 17 00:00:00 2001 From: 4Source <38220764+4Source@users.noreply.github.com> Date: Thu, 23 May 2024 22:29:31 +0200 Subject: [PATCH 1/5] Use obsidians funtion to extractFrontmatter --- src/util/utils.ts | 33 ++++++--------------------------- 1 file changed, 6 insertions(+), 27 deletions(-) diff --git a/src/util/utils.ts b/src/util/utils.ts index 6e4bfef8..db314274 100644 --- a/src/util/utils.ts +++ b/src/util/utils.ts @@ -1,6 +1,7 @@ import moment from "moment"; import { Moment } from "moment"; -import { PREFERRED_DATE_FORMAT, YAML_FRONT_MATTER_REGEX } from "src/constants"; +import { getFrontMatterInfo } from "obsidian"; +import { PREFERRED_DATE_FORMAT } from "src/constants"; type Hex = number; @@ -109,32 +110,10 @@ export function stringTrimStart(str: string): [string, string] { // e.g. for calls to getQuestionContext(cardLine: number) // 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; - } - } - - 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"); - } - } - if (frontmatter.length == 0) content = str; + let frontMatterInfo = getFrontMatterInfo(str); + let frontmatter: string = str.substring(0, frontMatterInfo.contentStart); + let content: string = str.substring(frontMatterInfo.contentStart); + return [frontmatter, content]; } From d825c3143917ccad64715ad85c5b0ecb3d665eaa Mon Sep 17 00:00:00 2001 From: 4Source <38220764+4Source@users.noreply.github.com> Date: Thu, 23 May 2024 23:04:30 +0200 Subject: [PATCH 2/5] Fix line pos shift --- src/util/utils.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/util/utils.ts b/src/util/utils.ts index db314274..3b38514e 100644 --- a/src/util/utils.ts +++ b/src/util/utils.ts @@ -90,6 +90,12 @@ export function splitTextIntoLineArray(text: string): string[] { return text.replaceAll("\r\n", "\n").split("\n"); } +export function getLineCount(text: string): number { + if(text.length === 0) + return 0; + return text.replaceAll("\r\n", "\n").split("\n").length; +} + export function stringTrimStart(str: string): [string, string] { const trimmed: string = str.trimStart(); const wsCount: number = str.length - trimmed.length; @@ -111,8 +117,13 @@ export function stringTrimStart(str: string): [string, string] { // export function extractFrontmatter(str: string): [string, string] { let frontMatterInfo = getFrontMatterInfo(str); - let frontmatter: string = str.substring(0, frontMatterInfo.contentStart); - let content: string = str.substring(frontMatterInfo.contentStart); + let frontmatter: string = str.substring(0, frontMatterInfo.contentStart - 1); + let frontmatterLineCount = getLineCount(frontmatter); + let content: string = ""; + for(let i = 0; i < frontmatterLineCount; i++) { + content += "\n"; + } + content += str.substring(frontMatterInfo.contentStart); return [frontmatter, content]; } From 8d79dcdade37cf64bb43f19c4604ff616e4c11cc Mon Sep 17 00:00:00 2001 From: 4Source <38220764+4Source@users.noreply.github.com> Date: Fri, 24 May 2024 21:57:23 +0200 Subject: [PATCH 3/5] Stop using obsidian function because of UnitTest --- src/util/utils.ts | 64 ++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/src/util/utils.ts b/src/util/utils.ts index 3b38514e..a076f325 100644 --- a/src/util/utils.ts +++ b/src/util/utils.ts @@ -1,6 +1,5 @@ import moment from "moment"; import { Moment } from "moment"; -import { getFrontMatterInfo } from "obsidian"; import { PREFERRED_DATE_FORMAT } from "src/constants"; type Hex = number; @@ -90,12 +89,6 @@ export function splitTextIntoLineArray(text: string): string[] { return text.replaceAll("\r\n", "\n").split("\n"); } -export function getLineCount(text: string): number { - if(text.length === 0) - return 0; - return text.replaceAll("\r\n", "\n").split("\n").length; -} - export function stringTrimStart(str: string): [string, string] { const trimmed: string = str.trimStart(); const wsCount: number = str.length - trimmed.length; @@ -103,28 +96,43 @@ export function stringTrimStart(str: string): [string, string] { return [ws, trimmed]; } -// -// 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 frontMatterInfo = getFrontMatterInfo(str); - let frontmatter: string = str.substring(0, frontMatterInfo.contentStart - 1); - let frontmatterLineCount = getLineCount(frontmatter); - let content: string = ""; - for(let i = 0; i < frontmatterLineCount; i++) { - content += "\n"; - } - content += str.substring(frontMatterInfo.contentStart); - + let lines = splitTextIntoLineArray(str); + let lineIndex = 0; + let hasFrontmatter = false; + do { + // Starts file with '---' + if (lineIndex === 0 && lines[lineIndex] === "---") { + hasFrontmatter = true; + } + // 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; + } + + let frontmatter: string = lines.slice(0, lineIndex).join("\n"); + let emptyLines: string[] = lineIndex > 0 ? Array(lineIndex).join(".").split(".") : []; + let content: string = emptyLines.concat(lines.slice(lineIndex)).join("\n"); + return [frontmatter, content]; } From 0b4ac9ebfe640370c8391dae71ec78b3a76e25e7 Mon Sep 17 00:00:00 2001 From: 4Source <38220764+4Source@users.noreply.github.com> Date: Fri, 24 May 2024 21:57:57 +0200 Subject: [PATCH 4/5] Add UnitTest for Frontmatter and Horizontal line --- tests/unit/util/utils.test.ts | 170 ++++++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) diff --git a/tests/unit/util/utils.test.ts b/tests/unit/util/utils.test.ts index bcfcf464..13add0bb 100644 --- a/tests/unit/util/utils.test.ts +++ b/tests/unit/util/utils.test.ts @@ -177,6 +177,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 + + + +Chemistry Question from file underdog 4B::goodby + + + +--- + +Chemistry Question from file underdog 4C::goodby + + + +This single {{question}} turns into {{3 separate}} {{cards}} + + + +---`; + + 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 + + + +Chemistry Question from file underdog 4B::goodby + + + +--- + +Chemistry Question from file underdog 4C::goodby + + + +This single {{question}} turns into {{3 separate}} {{cards}} + + + +--- +`; + + 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", + "", + "", + "", + "Chemistry Question from file underdog 4B::goodby", + "", + "", + "```", + "---", + "```", + "", + "Chemistry Question from file underdog 4C::goodby", + "", + "", + "", + "This single {{question}} turns into {{3 separate}} {{cards}}", + "", + "", + "", + "```", + "---", + "```", + ].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", () => { From 0d2715009fc0dabf88a0f75bf396d8d4f87f123f Mon Sep 17 00:00:00 2001 From: 4Source <38220764+4Source@users.noreply.github.com> Date: Fri, 24 May 2024 22:01:10 +0200 Subject: [PATCH 5/5] Fix linting --- src/util/utils.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/util/utils.ts b/src/util/utils.ts index a076f325..07c1d2c8 100644 --- a/src/util/utils.ts +++ b/src/util/utils.ts @@ -107,7 +107,7 @@ export function stringTrimStart(str: string): [string, string] { * @returns [frontmatter, content] */ export function extractFrontmatter(str: string): [string, string] { - let lines = splitTextIntoLineArray(str); + const lines = splitTextIntoLineArray(str); let lineIndex = 0; let hasFrontmatter = false; do { @@ -129,9 +129,9 @@ export function extractFrontmatter(str: string): [string, string] { lineIndex = 0; } - let frontmatter: string = lines.slice(0, lineIndex).join("\n"); - let emptyLines: string[] = lineIndex > 0 ? Array(lineIndex).join(".").split(".") : []; - let content: string = emptyLines.concat(lines.slice(lineIndex)).join("\n"); + 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]; }