Skip to content

Commit

Permalink
fix: Markdown splitter is not greedy
Browse files Browse the repository at this point in the history
- Fix the open block regex to allow space before the language selector (js)
- Fix the parser to extract more than only the first code block 
  (currently, the parser fails to find more than one code block)
  • Loading branch information
nopnop committed Sep 6, 2016
1 parent e95cab8 commit a18bb24
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions lib/utils/markdown-splitter.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
module.exports = function (fileContent) {
var startFencedCodeRegex = /(```js[ ]*)$/gm
var stopFencedCodeRegex = /(```[ ]*)$/gm
var startFencedCodeRegex = /(```\s*js\s*)$/gm
var stopFencedCodeRegex = /(```\s*)$/gm
var blocks = []
var lineTracker = 1
var offset = 0

var splitterRecursive = function () {
startFencedCodeRegex.lastIndex = offset

var start = startFencedCodeRegex.exec(fileContent)

if (start) {
var contentCutted = fileContent.substring(0, start.index)
fileContent = fileContent.substring(start.index)

stopFencedCodeRegex.lastIndex = contentCutted.length

var end = stopFencedCodeRegex.exec(fileContent)

if (!end) {
return false
}

lineTracker += contentCutted.split('\n').length - 1

var line = lineTracker
var content = fileContent.substring(start[0].length + 1, end.index - 1)
var content = fileContent.substring(contentCutted.length + start[0].length, end.index).trimRight() + '\n'
offset = end.index

blocks.push({
line: line,
line: contentCutted.split('\n').length - 1,
content: content
})

Expand Down

0 comments on commit a18bb24

Please sign in to comment.