Skip to content

Commit

Permalink
Support for empty Markdown code block (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
gucong3000 committed Mar 14, 2018
1 parent 540ada3 commit 22a34d0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lib/markdown-parser.js
Expand Up @@ -13,7 +13,7 @@ module.exports = function (source, opts) {
const blocks = findAllAfter(ast, 0, (node) => {
if (node.type === "code" && node.lang) {
isStructureMD = true;
return /^(?:(?:[ps]?c)|le|wx|sa?|sugar)ss$/i.test(node.lang);
return /^(?:(?:\w*c)|le|wx|sa?|sugar)ss$/i.test(node.lang);
}
});

Expand All @@ -22,7 +22,12 @@ module.exports = function (source, opts) {
}

return blocks.map((block) => {
const startIndex = source.indexOf(block.value, block.position.start.offset);
let startIndex = source.indexOf(block.lang, block.position.start.offset) + block.lang.length;
if (block.value) {
startIndex = source.indexOf(block.value, startIndex);
} else {
startIndex = source.indexOf("\n", startIndex) + 1;
}
const content = source.slice(startIndex, block.position.end.offset).replace(/[ \t]*`*$/, "");
return {
startIndex: startIndex,
Expand Down
25 changes: 24 additions & 1 deletion test/markdown.js
Expand Up @@ -45,7 +45,30 @@ describe("markdown tests", () => {
});
});

it("Without code blocks", function () {
it("empty code block", () => {
const source = [
"hi",
"",
"```css",
"",
"```",
"",
].join("\n");
return postcss([
root => {
const css = root.nodes[0].source;
console.log(css);
expect(css.input.css).equal("\n");
expect(css.start.line).equal(4);
expect(css.start.column).equal(1);
},
]).process(source, {
syntax: syntax,
from: "empty_code_block.md",
});
});

it("without code blocks", function () {
return postcss([
root => {
expect(root.nodes).to.have.lengthOf(0);
Expand Down

0 comments on commit 22a34d0

Please sign in to comment.