Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yang Zhao
committed
Sep 2, 2020
1 parent
58b6945
commit 6fbf581
Showing
6 changed files
with
114 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Transform empty lines into empty <p> tags | ||
// empty lines data comes from state.env.emptyLines, | ||
|
||
export default function empty_lines_block(state) { | ||
var i, ln; | ||
var emptyLines = state.env.emptyLines; | ||
if (!emptyLines || state.options.emptyLines !== true) { | ||
return; | ||
} | ||
var tokens = state.tokens; | ||
var pendingTokens = []; | ||
var lastVisitedIndex = 0; | ||
for (var lineNumber in emptyLines) { | ||
for (i = lastVisitedIndex; i < tokens.length; i++) { | ||
var token = tokens[i]; | ||
ln = Number(lineNumber); | ||
// find the first "paragraph" that after the current empty lines | ||
if ( | ||
token.type === 'paragraph_open' && | ||
token.lines && | ||
token.lines[0] >= ln | ||
) { | ||
// push the index info of the found "paragraph" | ||
pendingTokens.push({ | ||
index: i, | ||
lineNumber: ln, | ||
level: token.level | ||
}); | ||
lastVisitedIndex = ln; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
// insert the empty line from last to first | ||
while (pendingTokens.length > 0) { | ||
var t = pendingTokens.pop(); | ||
var idx = t.index, lvl = t.level; | ||
ln = t.lineNumber; | ||
for (i = 0; i < emptyLines[ln] - 1; i++) { | ||
tokens.splice( | ||
idx, | ||
0, | ||
{ | ||
type: 'paragraph_open', | ||
tight: false, | ||
lines: [ln + i, ln + i + emptyLines[ln]], | ||
level: lvl | ||
}, | ||
{ | ||
type: 'paragraph_close', | ||
tight: false, | ||
level: lvl | ||
} | ||
); | ||
} | ||
} | ||
|
||
state.tokens = tokens; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
import assert from 'assert'; | ||
import { Remarkable } from '../lib/index'; | ||
|
||
describe('Test empty lines plugin', function() { | ||
it('should render with empty lines when enabled', function() { | ||
[ | ||
['', ''], | ||
['abc\n\ndef\n', '<p>abc</p>\n<p>def</p>\n'], | ||
['abc\n\n\ndef\n', '<p>abc</p>\n<p>def</p>\n'], | ||
['abc\n\n\n\ndef\n\n\n\nghi\n', '<p>abc</p>\n<p></p>\n<p>def</p>\n<p></p>\n<p>ghi</p>\n'], | ||
['line1\n\n\n\nline3\n', '<p>line1</p>\n<p></p>\n<p>line3</p>\n'], | ||
['* line1\n* line2\n\n\nline4\n', '<ul>\n<li>line1</li>\n<li>line2</li>\n</ul>\n<p>line4</p>\n'] | ||
].forEach(function(data) { | ||
var [text, expected] = data; | ||
var md = new Remarkable('full', { emptyLines: true }); | ||
var rendered = md.render(text); | ||
assert.strictEqual(rendered, expected); | ||
}); | ||
}); | ||
|
||
it('should render without empty lines when disabled', function() { | ||
[ | ||
['', ''], | ||
['abc\n\ndef\n', '<p>abc</p>\n<p>def</p>\n'], | ||
['abc\n\n\ndef\n', '<p>abc</p>\n<p>def</p>\n'], | ||
['line1\n\n\n\nline3\n', '<p>line1</p>\n<p>line3</p>\n'], | ||
['* line1\n* line2\n\n\n\n\n\n\nline4\n', '<ul>\n<li>line1</li>\n<li>line2</li>\n</ul>\n<p>line4</p>\n'] | ||
].forEach(function(data) { | ||
var [text, expected] = data; | ||
var md = new Remarkable('full'); | ||
var rendered = md.render(text); | ||
assert.strictEqual(rendered, expected); | ||
}); | ||
}); | ||
}); |