diff --git a/.gitignore b/.gitignore index 5171c54..ae3912a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules -npm-debug.log \ No newline at end of file +npm-debug.log +test/fixure/build diff --git a/lib/index.js b/lib/index.js index 0f15f14..c03fe9f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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('
' + hljs.highlightAuto(code).value + '
'); } else { @@ -87,4 +90,4 @@ function plugin(options) { debug(files[file].contents.toString()); }); }; -} \ No newline at end of file +} diff --git a/test/fixture/build/csharp.md b/test/fixture/build/csharp.md deleted file mode 100644 index 49432cb..0000000 --- a/test/fixture/build/csharp.md +++ /dev/null @@ -1,25 +0,0 @@ -
using System;
-
-#pragma warning disable 414, 3021
-
-public class Program
-{
-    /// <summary>The entry point to the program.</summary>
-    public static int Main(string[] args)
-    {
-        Console.WriteLine("Hello, World!");
-        string s = @"This
-""string""
-spans
-multiple
-lines!";
-        return 0;
-    }
-}
-
-async Task<int> AccessTheWebAsync()
-{
-    // ...
-    string urlContents = await getStringTask;
-    return urlContents.Length;
-}
\ No newline at end of file diff --git a/test/fixture/build/index.md b/test/fixture/build/index.md deleted file mode 100644 index 5df6466..0000000 --- a/test/fixture/build/index.md +++ /dev/null @@ -1,10 +0,0 @@ -Non code - -
local var = "untyped code"
- -
var t = 'typed code'
- -
var t = 'typed code with a space'
- - -More non code diff --git a/test/fixture/build/nohighlight.md b/test/fixture/build/nohighlight.md deleted file mode 100644 index cc36364..0000000 --- a/test/fixture/build/nohighlight.md +++ /dev/null @@ -1,3 +0,0 @@ -Non code - -
var t = 'typed code'
\ No newline at end of file diff --git a/test/fixture/expected/embedded-markers.md b/test/fixture/expected/embedded-markers.md new file mode 100644 index 0000000..1d86fe2 --- /dev/null +++ b/test/fixture/expected/embedded-markers.md @@ -0,0 +1,8 @@ +The following syntax should work using this plugin: + +
var myCodeBlock = '```' +
+  'some-code' +
+  '```';
+done();
+ +And be a single js code block diff --git a/test/fixture/src/embedded-markers.md b/test/fixture/src/embedded-markers.md new file mode 100644 index 0000000..b53d854 --- /dev/null +++ b/test/fixture/src/embedded-markers.md @@ -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 diff --git a/test/index.js b/test/index.js index 27aeca3..8e41c1d 100644 --- a/test/index.js +++ b/test/index.js @@ -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(); + }); + }); + }); });