Skip to content

Commit

Permalink
Merge pull request MSzturc#47 from miluoshi/main
Browse files Browse the repository at this point in the history
fix: ignore unordered lists in code blocks from parsing as a fragmented list
  • Loading branch information
MSzturc authored Mar 25, 2022
2 parents 19daa5b + 8084713 commit 61015a2
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 37 deletions.
47 changes: 26 additions & 21 deletions src/processors/fragmentProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,42 @@ export class FragmentProcessor {
private parser: CommentParser;
private fragmentCounter = 1;
private orderedListRegex = /\d\) /g;
private codeBlockRegex = /```[^\n]*(?:\n[^`]*\n)```/g;

constructor() {
this.parser = new CommentParser();
}

process(markdown: string, options: Options) {
let output = markdown;
const separatorRegexp = new RegExp(`${options.separator}|${options.verticalSeparator}`, 'gmi');

markdown
.split(new RegExp(options.separator, 'gmi'))
.map((slidegroup) => {
return slidegroup
.split(new RegExp(options.verticalSeparator, 'gmi'))
.map((slide) => {
this.fragmentCounter = 1;
// Detect line ranges containing Markdown code blocks so we can ignore them
const codeBlockLines = Array.from(markdown.matchAll(this.codeBlockRegex))
.map(({ 0: match, index }) => ({
from: markdown.substring(0, index).split('\n').length - 1,
to: markdown.substring(0, index + match.length).split('\n').length - 1
}));

const newSlide = slide.split('\n')
.map((line) => {
if (line.trim().startsWith("+ ") || this.orderedListRegex.test(line.trim())) {
return this.transformLine(line);
}
return line;
})
.join('\n');
output = output.split(slide).join(newSlide);
return newSlide;
const output = markdown
.split('\n')
.map((line, lineNumber) => {
if (`\n${line}\n`.match(separatorRegexp)) {
// Reset counter when encountered slide separator
this.fragmentCounter = 1;
return line;
}

})
.join(options.verticalSeparator);
const isCodeblockLine = codeBlockLines.some(({ from, to }) => lineNumber >= from && lineNumber <= to);
if (isCodeblockLine) {
return line;
}

if (line.trim().startsWith("+ ") || this.orderedListRegex.test(line.trim())) {
return this.transformLine(line);
}
return line;
})
.join(options.separator);
.join('\n');

return output;
}
Expand Down
12 changes: 12 additions & 0 deletions test/__snapshots__/extendedSyntax.unit.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ exports[`Extended Markdown Syntax > Fragmented list 1`] = `
---
# Fragmented unordered list with ignored code block
- First
- Second<!-- .element: class=\\"fragment\\" data-fragment-index=\\"1\\" -->
\`\`\`diff
- ignored block text 1
+ ignored block text 2
\`\`\`
---
# Ordered list
1. First
Expand Down
30 changes: 15 additions & 15 deletions test/comment.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CommentParser, Comment } from "src/comment";
import { YamlStore } from "src/yamlStore";
import { getSlideOptions } from "./testUtils";

test('Parse Coment', () => {
test('Parse Comment', () => {
YamlStore.getInstance().options = getSlideOptions({})
const parser = new CommentParser();

Expand All @@ -17,7 +17,7 @@ test('Parse Coment', () => {
expect(parsed).toStrictEqual(expected);
});

test('Parse Coment', () => {
test('Parse Comment', () => {
YamlStore.getInstance().options = getSlideOptions({})

const parser = new CommentParser();
Expand Down Expand Up @@ -48,7 +48,7 @@ test('Empty Slide comment', () => {
expect(parsed).toStrictEqual(expected);
});

test('Parse Coment', () => {
test('Parse Comment', () => {
YamlStore.getInstance().options = getSlideOptions({})

const parser = new CommentParser();
Expand All @@ -65,7 +65,7 @@ test('Parse Coment', () => {
expect(parsed).toStrictEqual(expected);
});

test('Parse Coment', () => {
test('Parse Comment', () => {
YamlStore.getInstance().options = getSlideOptions({})

const parser = new CommentParser();
Expand All @@ -80,7 +80,7 @@ test('Parse Coment', () => {
expect(parsed).toStrictEqual(expected);
});

test('Parse Coment with bg property', () => {
test('Parse Comment with bg property', () => {
YamlStore.getInstance().options = getSlideOptions({})

const parser = new CommentParser();
Expand All @@ -97,7 +97,7 @@ test('Parse Coment with bg property', () => {
expect(parsed).toStrictEqual(expected);
});

test('Parse Coment with bg property', () => {
test('Parse Comment with bg property', () => {
YamlStore.getInstance().options = getSlideOptions({})

const parser = new CommentParser();
Expand All @@ -110,12 +110,12 @@ test('Parse Coment with bg property', () => {
[],
['has-dark-background'],
new Map<string, string>([["data-background-color","black"]])

);
expect(parsed).toStrictEqual(expected);
});

test('Merge Coment', () => {
test('Merge Comment', () => {
YamlStore.getInstance().options = getSlideOptions({})

const parser = new CommentParser();
Expand Down Expand Up @@ -143,7 +143,7 @@ test('Invalid input', () => {
expect(parsed).toBeNull();
});

test('Parse Coment with pad property', () => {
test('Parse Comment with pad property', () => {
YamlStore.getInstance().options = getSlideOptions({})

const parser = new CommentParser();
Expand All @@ -158,7 +158,7 @@ test('Parse Coment with pad property', () => {
expect(parsed).toStrictEqual(expected);
});

test('Parse Coment with animate property', () => {
test('Parse Comment with animate property', () => {
YamlStore.getInstance().options = getSlideOptions({})

const parser = new CommentParser();
Expand All @@ -174,7 +174,7 @@ test('Parse Coment with animate property', () => {
expect(parsed).toStrictEqual(expected);
});

test('Parse Coment with fragment property', () => {
test('Parse Comment with fragment property', () => {
YamlStore.getInstance().options = getSlideOptions({})

const parser = new CommentParser();
Expand All @@ -191,7 +191,7 @@ test('Parse Coment with fragment property', () => {
expect(parsed).toStrictEqual(expected);
});

test('Parse Coment with opacity property', () => {
test('Parse Comment with opacity property', () => {
YamlStore.getInstance().options = getSlideOptions({})

const parser = new CommentParser();
Expand All @@ -206,7 +206,7 @@ test('Parse Coment with opacity property', () => {
expect(parsed).toStrictEqual(expected);
});

test('Parse Coment with border property', () => {
test('Parse Comment with border property', () => {
YamlStore.getInstance().options = getSlideOptions({})

const parser = new CommentParser();
Expand All @@ -221,7 +221,7 @@ test('Parse Coment with border property', () => {
expect(parsed).toStrictEqual(expected);
});

test('Parse Coment with rotate property', () => {
test('Parse Comment with rotate property', () => {
YamlStore.getInstance().options = getSlideOptions({})

const parser = new CommentParser();
Expand All @@ -237,7 +237,7 @@ test('Parse Coment with rotate property', () => {

});

test('Parse Coment with rotate property', () => {
test('Parse Comment with rotate property', () => {
YamlStore.getInstance().options = getSlideOptions({})

const parser = new CommentParser();
Expand Down
14 changes: 13 additions & 1 deletion test/extendedSyntax.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ note: this is not! Only the speaker might see this text.
- and this bulletpoint
- or this picture
![](https://picsum.photos/id/1005/250/250)
![](https://picsum.photos/id/1005/250/250)
`;

const { options, markdown } = prepare(input);
Expand Down Expand Up @@ -240,6 +240,18 @@ test('Extended Markdown Syntax > Fragmented list', () => {
---
# Fragmented unordered list with ignored code block
- First
+ Second
\`\`\`diff
- ignored block text 1
+ ignored block text 2
\`\`\`
---
# Ordered list
1. First
Expand Down

0 comments on commit 61015a2

Please sign in to comment.