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 Blockquotes Merging When Blank Blockquote Line Between 2 Blockquotes #705

Merged
merged 4 commits into from
May 5, 2023
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
38 changes: 37 additions & 1 deletion __tests__/empty-line-around-blockquotes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ruleTest({
`,
},
{ // accounts for https://github.com/platers/obsidian-linter/issues/668
testname: 'Make sure that consecutive blockquotes are not merged when multiple blank lines are between them',
testName: 'Make sure that consecutive blockquotes are not merged when multiple blank lines are between them',
before: dedent`
> [!quote] title 1
> the quote 1
Expand Down Expand Up @@ -90,5 +90,41 @@ ruleTest({
> The quote 3
`,
},
{ // accounts for https://github.com/platers/obsidian-linter/issues/684
testName: 'Make sure that consecutive blockquotes do not get merged when the first one ends with an empty blockquote line',
before: dedent`
> [!FAQ] Title
>
${''}
> [!NOTES] Title
> Content
`,
after: dedent`
> [!FAQ] Title
>
${''}
> [!NOTES] Title
> Content
`,
},
{ // accounts for https://github.com/platers/obsidian-linter/issues/684
testName: 'Make sure that consecutive blockquotes do not get merged when the second one starts with an empty blockquote line',
before: dedent`
> [!FAQ] Title
> Content here
${''}
>
> [!NOTES] Title
> Content
`,
after: dedent`
> [!FAQ] Title
> Content here
${''}
>
> [!NOTES] Title
> Content
`,
},
],
});
25 changes: 25 additions & 0 deletions src/utils/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,33 @@ function makeSureContentHasASingleEmptyLineBeforeItUnlessItStartsAFileForBlockqu
let index = startOfContent;
let startOfNewContent = startOfContent;
let lineNestingLevel = 0;
let foundABlankLine = false;
let previousChar = '';
while (index >= 0) {
const currentChar = text.charAt(index);
if (currentChar.trim() !== '' && currentChar !== '>') {
break; // if non-whitespace, non-gt-bracket is encountered, then the line has content
} else if (currentChar === '>') {
// if we go from having a blank line at any point to then having more blockquote content we know we have encountered another blockquote
if (foundABlankLine) {
break;
}

lineNestingLevel++;
} else if (currentChar === '\n') {
if (lineNestingLevel === 0 || lineNestingLevel === nestingLevel || (lineNestingLevel + 1) === nestingLevel) {
startOfNewContent = index;
lineNestingLevel = 0;

if (previousChar === '\n') {
foundABlankLine = true;
}
} else {
break;
}
}
index--;
previousChar = currentChar;
}

if (index < 0 || startOfNewContent === 0) {
Expand Down Expand Up @@ -169,11 +181,18 @@ function makeSureContentHasASingleEmptyLineAfterItUnlessItEndsAFileForBlockquote
let endOfNewContent = endOfContent;
let isFirstNewLine = true;
let lineNestingLevel = 0;
let foundABlankLine = false;
let previousChar = '';
while (index < text.length) {
const currentChar = text.charAt(index);
if (currentChar.trim() !== '' && currentChar !== '>') {
break; // if non-whitespace is encountered, then the line has content
} else if (currentChar === '>') {
// if we go from having a blank line at any point to then having more blockquote content we know we have encountered another blockquote
if (foundABlankLine) {
break;
}

lineNestingLevel++;
} else if (currentChar === '\n') {
if (lineNestingLevel === 0 || lineNestingLevel === nestingLevel || (lineNestingLevel + 1) === nestingLevel) {
Expand All @@ -183,11 +202,17 @@ function makeSureContentHasASingleEmptyLineAfterItUnlessItEndsAFileForBlockquote
} else {
endOfNewContent = index;
}

if (previousChar === '\n') {
foundABlankLine = true;
}
} else {
break;
}
}
index++;

previousChar = currentChar;
}

if (index === text.length || endOfNewContent === text.length - 1) {
Expand Down