Skip to content

Commit

Permalink
Fix regex to match only lines starting with whitespace (#10)
Browse files Browse the repository at this point in the history
Adds test; also updates travis to no longer test on unsupported node versions, and removes the test build output from the repository (and adds it to gitignore)
  • Loading branch information
weswigham authored Oct 18, 2016
1 parent 5a4847b commit 574c266
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 50 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
npm-debug.log
npm-debug.log
test/fixure/build
25 changes: 14 additions & 11 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,37 @@ function plugin(options) {
if (!markdown(file)) { return; }
var data = files[file],
str = data.contents.toString(),
end = /```(.*)/,
end = /^(\s*)```(.*)/m,
remainder = str,
pos = 0,
endpos = 0,
replacements = [];

while (end.test(remainder)) {
var match = remainder.match(end),
candidate = match[1].trim(),
lang = candidate.length > 0 ? candidate : null,
var match = end.exec(remainder),
candidate = match[2] && match[2].trim(),
lang = (candidate && candidate.length) > 0 ? candidate : null,
prev = pos,
buff = 3 + (lang ? match[1].length : 0);
buff = match[0].length,
leadingSpaceLength = match[1] ? match[1].length : 0;

pos = pos + match.index + buff;
remainder = str.substring(pos);

var ends = remainder.match(end);
if (ends === null || ends[0] === null) {
var ends = end.exec(remainder);
if (ends === null) {
break;
}

endpos = ends.index;
replacements.push(str.substring(prev, pos - buff));
replacements.push(str.substring(prev, pos - buff + leadingSpaceLength));

remainder = str.substring(pos + endpos + 3);
var fencePrefixLength = (ends[1] ? ends[1].length : 0) + 3;
var nextPos = pos + endpos + fencePrefixLength;
remainder = str.substring(nextPos);
var code = str.substring(pos, pos + endpos);
code = code.trim('(\r\n|\n)');
pos = pos + endpos + 3;
pos = nextPos;
if (lang === null) {
replacements.push('<pre><code class="hljs">' + hljs.highlightAuto(code).value + '</code></pre>');
} else {
Expand All @@ -87,4 +90,4 @@ function plugin(options) {
debug(files[file].contents.toString());
});
};
}
}
25 changes: 0 additions & 25 deletions test/fixture/build/csharp.md

This file was deleted.

10 changes: 0 additions & 10 deletions test/fixture/build/index.md

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixture/build/nohighlight.md

This file was deleted.

8 changes: 8 additions & 0 deletions test/fixture/expected/embedded-markers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
The following syntax should work using this plugin:

<pre><code class="hljs javascript"><span class="hljs-keyword">var</span> myCodeBlock = <span class="hljs-string">'```'</span> +
<span class="hljs-string">'some-code'</span> +
<span class="hljs-string">'```'</span>;
done();</code></pre>

And be a single js code block
10 changes: 10 additions & 0 deletions test/fixture/src/embedded-markers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
The following syntax should work using this plugin:

```javascript
var myCodeBlock = '```' +
'some-code' +
'```';
done();
```

And be a single js code block
15 changes: 15 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,19 @@ describe('metalsmith-metallic', function(){
});
});
});

it('should handle embedded fences correctly', function(done){
fs.readFile('test/fixture/expected/embedded-markers.md', 'utf8', function (err,data) {
if (err) {
return console.log(err)
}
Metalsmith('test/fixture')
.use(metallic())
.build(function(err, files){
if (err) { return done(err); }
assert.equal(files['embedded-markers.md'].contents.toString(), data.toString());
done();
});
});
});
});

0 comments on commit 574c266

Please sign in to comment.