build(aio): throw an error if a code-example tag is not closed

This commit is contained in:
Peter Bacon Darwin 2017-05-04 22:02:26 +01:00 committed by Matias Niemelä
parent 9449eff6fd
commit 5057e16874
2 changed files with 46 additions and 29 deletions

View File

@ -91,6 +91,7 @@ module.exports = function renderMarkdown() {
const openMatch = anyBlockMatcher.exec(value);
if (openMatch) {
const blockName = openMatch[1];
try {
const fullMatch = matchRecursiveRegExp(value, createOpenMatcher(blockName), createCloseMatcher(blockName))[0];
if (silent || !fullMatch) {
// either we are not eating (silent) or the match failed
@ -100,6 +101,9 @@ module.exports = function renderMarkdown() {
type: 'html',
value: fullMatch[0]
});
} catch(e) {
this.file.fail('Unmatched plain HTML block tag ' + e.message);
}
}
}
}
@ -161,7 +165,6 @@ function rgxFindMatchPos(str, left, right, flags) {
let index, match, start, end;
let count = 0;
do {
while ((match = bothMatcher.exec(str))) {
if (leftMatcher.test(match[0])) {
if (!(count++)) {
@ -184,7 +187,10 @@ function rgxFindMatchPos(str, left, right, flags) {
}
}
}
} while (count && (bothMatcher.lastIndex = index));
if (count) {
throw new Error(str.slice(start, index));
}
return pos;
}

View File

@ -49,6 +49,17 @@ describe('remark: renderMarkdown service', () => {
expect(output).toEqual('<code-example>\n\n **abc**\n\n def\n</code-example>\n<code-tabs><code-pane>\n\n **abc**\n\n def\n</code-pane></code-tabs>\n');
});
it('should handle recursive tags marked as unformatted', () => {
const content = '<code-example>\n\n <code-example>\n\n **abc**\n\n def\n\n</code-example>\n\n</code-example>\n\nhij\n\n<code-example>\n\nklm</code-example>';
const output = renderMarkdown(content);
expect(output).toEqual('<code-example>\n\n <code-example>\n\n **abc**\n\n def\n\n</code-example>\n\n</code-example>\n<p>hij</p>\n<code-example>\n\nklm</code-example>\n');
});
it('should raise an error if a tag marked as unformatted is not closed', () => {
const content = '<code-example path="xxx">\n\n **abc**\n\n def\n<code-example>\n\n\n\n **abc**\n\n def\n</code-example>';
expect(() => renderMarkdown(content)).toThrowError('Unmatched plain HTML block tag <code-example path="xxx">');
});
it('should not remove spaces after anchor tags', () => {
var input =
'A aa aaa aaaa aaaaa aaaaaa aaaaaaa aaaaaaaa aaaaaaaaa aaaaaaaaaa aaaaaaaaaaa\n' +