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

Refactor Logic Around Blank Line Being Put Around Content #658

Merged
merged 7 commits into from
Mar 18, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
import {makeSureContentHasEmptyLinesAddedBeforeAndAfter} from '../src/utils/strings';
import dedent from 'ts-dedent';

type EmptyStringBeforeAndAfterTestCase = {
testName: string,
startOfContent: number,
endOfContent: number
before: string,
after: string,
}

const testCases: EmptyStringBeforeAndAfterTestCase[] = [
{
testName: 'Content with no empty line before or after it should have one added before and after',
startOfContent: 9,
endOfContent: 26,
before: dedent`
# Header
Text content here
## Other Header
`,
after: dedent`
# Header
${''}
Text content here
${''}
## Other Header
`,
},
{
testName: 'Content with multiple empty lines before it should have one added before and after',
startOfContent: 13,
endOfContent: 29,
before: dedent`
# Header
${''}
${''}
${''}
Text content here
${''}
## Other Header
`,
after: dedent`
# Header
${''}
Text content here
${''}
## Other Header
`,
},
{
testName: 'Content at the start of the file should not get an empty line added before it',
startOfContent: 0,
endOfContent: 17,
before: dedent`
Text content here
${''}
## Other Header
`,
after: dedent`
Text content here
${''}
## Other Header
`,
},
{
testName: 'Content at the end of the file should not get an empty line added after it',
startOfContent: 9,
endOfContent: 26,
before: dedent`
# Header
Text content here
`,
after: dedent`
# Header
${''}
Text content here
`,
},
{
testName: 'Content in blockquote has en empty line added before and after it',
startOfContent: 11,
endOfContent: 28,
before: dedent`
# Header
> Text content here
More unrelated content here
`,
after: dedent`
# Header
${''}
> Text content here
${''}
More unrelated content here
`,
},
{
testName: 'Content in blockquote that is surrounded by more content in the same blockquote does not end the blockquote',
startOfContent: 64,
endOfContent: 81,
before: dedent`
# Header
> Content prior to content to put empty lines around
> Text content here
> Content after content to put empty lines around
`,
after: dedent`
# Header
> Content prior to content to put empty lines around
>
> Text content here
>
> Content after content to put empty lines around
`,
},
{
testName: 'A table in a blockquote or callout should have a blank line added before and after the table',
startOfContent: 24, // start of the table header: "|"
endOfContent: 206, // end of the table "\n"
before: dedent`
> Table in blockquote
> | Column 1 | Column 2 | Column 3 |
> |----------|----------|----------|
> | foo | bar | blob |
> | baz | qux | trust |
> | quux | quuz | glob |
${''}
More content here
`,
after: dedent`
> Table in blockquote
>
> | Column 1 | Column 2 | Column 3 |
> |----------|----------|----------|
> | foo | bar | blob |
> | baz | qux | trust |
> | quux | quuz | glob |
${''}
More content here
`,
},
{
testName: 'A table in a nested blockquote or callout should have a blank line added before and after the table',
startOfContent: 59, // start of the table header: "|"
endOfContent: 249, // end of the table "\n"
before: dedent`
More content here
${''}
> Table doubly nested in blockquote
> > | Column 1 | Column 2 | Column 3 |
> > |----------|----------|----------|
> > | foo | bar | blob |
> > | baz | qux | trust |
> > | quux | quuz | glob |
More content here
`,
after: dedent`
More content here
${''}
> Table doubly nested in blockquote
>
> > | Column 1 | Column 2 | Column 3 |
> > |----------|----------|----------|
> > | foo | bar | blob |
> > | baz | qux | trust |
> > | quux | quuz | glob |
${''}
More content here
`,
},
{
startOfContent: 124, // start of the 2nd code block: "`"
endOfContent: 175, // end of the 2nd code block: "\n"
testName: 'Fenced code blocks that are in a blockquote have the proper empty line added',
before: dedent`
# Make sure that code blocks in blockquotes are accounted for correctly
> \`\`\`js
> var text = 'this is some text';
> \`\`\`
>
> \`\`\`js
> var other text = 'this is more text';
> \`\`\`
${''}
**Note that the blanks blockquote lines added do not have whitespace after them**
${''}
# Doubly nested code block
${''}
> > \`\`\`js
> > var other text = 'this is more text';
> > \`\`\`
`,
after: dedent`
# Make sure that code blocks in blockquotes are accounted for correctly
> \`\`\`js
> var text = 'this is some text';
> \`\`\`
>
> \`\`\`js
> var other text = 'this is more text';
> \`\`\`
${''}
**Note that the blanks blockquote lines added do not have whitespace after them**
${''}
# Doubly nested code block
${''}
> > \`\`\`js
> > var other text = 'this is more text';
> > \`\`\`
`,
},
];


describe('Make Sure Content Has Empty Lines Added Before and After', () => {
for (const testCase of testCases) {
it(testCase.testName, () => {
// console.log(testCase.testName, testCase.before.indexOf('Text content here')); // helpful for adding new tests
expect(makeSureContentHasEmptyLinesAddedBeforeAndAfter(testCase.before, testCase.startOfContent, testCase.endOfContent)).toStrictEqual(testCase.after);
});
}
});
4 changes: 1 addition & 3 deletions src/rules/empty-line-around-code-fences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,19 @@ export default class EmptyLineAroundCodeFences extends RuleBuilder<EmptyLineArou
`,
after: dedent`
# Make sure that code blocks in blockquotes are accounted for correctly
>

> \`\`\`js
> var text = 'this is some text';
> \`\`\`
>
> \`\`\`js
> var other text = 'this is more text';
> \`\`\`
>
${''}
**Note that the blanks blockquote lines added do not have whitespace after them**
${''}
# Doubly nested code block
${''}
> >
> > \`\`\`js
> > var other text = 'this is more text';
> > \`\`\`
Expand Down
3 changes: 1 addition & 2 deletions src/rules/empty-line-around-math-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,11 @@ export default class EmptyLineAroundMathBlock extends RuleBuilder<EmptyLineAroun
> $$
> \\boldsymbol{a}=\\begin{bmatrix}a_x \\\\ a_y\\end{bmatrix}
> $$
>
${''}
More content here
${''}
> Math block doubly nested in blockquote
> >
>
> > $$
> > \\boldsymbol{a}=\\begin{bmatrix}a_x \\\\ a_y\\end{bmatrix}
> > $$
Expand Down
3 changes: 1 addition & 2 deletions src/rules/empty-line-around-tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,11 @@ export default class EmptyLineAroundTables extends RuleBuilder<EmptyLineAroundTa
> | foo | bar | blob |
> | baz | qux | trust |
> | quux | quuz | glob |
>
${''}
More content here
${''}
> Table doubly nested in blockquote
> >
>
> > | Column 1 | Column 2 | Column 3 |
> > |----------|----------|----------|
> > | foo | bar | blob |
Expand Down
8 changes: 7 additions & 1 deletion src/utils/mdast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,13 @@ export function ensureEmptyLinesAroundMathBlock(text: string, numberOfDollarSign
export function ensureEmptyLinesAroundBlockquotes(text: string): string {
const positions: Position[] = getPositions(MDAstTypes.Blockquote, text);
for (const position of positions) {
text = makeSureContentHasEmptyLinesAddedBeforeAndAfter(text, position.start.offset, position.end.offset, true);
// make sure to shift end to the next new line character just in case blocquotes are nested which can cause changes to move content out of the original position expected
let endIndex = position.end.offset;
while (endIndex < text.length - 1 && text.charAt(endIndex) !== '\n') {
endIndex++;
}

text = makeSureContentHasEmptyLinesAddedBeforeAndAfter(text, position.start.offset, endIndex);
}

return text;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/regex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export function ensureEmptyLinesAroundTables(text: string): string {
}

for (const table of tableMatches) {
let start = text.indexOf(table);
let start = text.indexOf(table.trimStart());
const end = start + table.length;
if (table.trim().startsWith('>')) {
while (text.charAt(start).trim() === '' || text.charAt(start) === '>') {
Expand Down
Loading