Skip to content

Commit

Permalink
fix(compiler): add additional breaks after lists
Browse files Browse the repository at this point in the history
There should be an empty line after a list if it is the last block in a MarkdownScript. Otherwise
the closing MarkdownScript tag will be parsed as part of the list.
  • Loading branch information
zkochan committed May 1, 2016
1 parent 39c6c1a commit d5b854e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ module.exports = attacher

const createAsyncScopeEval = require('./create-async-scope-eval')

function createInsertBlock (code, body) {
const bodyWithNL = body ? `\n${body}\n` : body
return `<!--@${code}-->${bodyWithNL}<!--/@-->`
}

const template = new RegExp('^<!--@([\\s\\S]+?)-->(?:[\\s\\S]*?)<!--/@-->')

function matchRecursive (str) {
Expand Down Expand Up @@ -79,7 +74,12 @@ function attacher (remark, opts) {
const methods = proto.inlineMethods
const blockMethods = proto.blockMethods
remark.Compiler.prototype.visitors.markdownScript = function (node) {
return createInsertBlock(node.code, this.block(node))
const body = this.block(node)
let bodyWithNL = body ? `\n${body}\n` : body
if (node.children && node.children[node.children.length - 1].type === 'list') {
bodyWithNL += '\n'
}
return `<!--@${node.code}-->${bodyWithNL}<!--/@-->`
}

proto.inlineTokenizers.markdownScript = markdownScript
Expand Down
24 changes: 24 additions & 0 deletions lib/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,28 @@ describe('mos', () => {
expect(newMD).to.eq('Insert <!--@ ast -->\nfoo\n<!--/@--> here\n')
})
})

it('should insert additional break after list', () => {
const renderMD = mdRenderer({
ast: {
type: 'list',
ordered: false,
children: [
{
type: 'listItem',
children: [
{
type: 'text',
value: 'foo',
},
],
},
],
},
})
return renderMD('Insert <!--@ ast --><!--/@--> here')
.then(newMD => {
expect(newMD).to.eq('Insert <!--@ ast -->\n- foo\n\n<!--/@--> here\n')
})
})
})

0 comments on commit d5b854e

Please sign in to comment.