diff options
| -rw-r--r-- | lib/writers/markdown-writer.js | 18 | ||||
| -rw-r--r-- | tests/writers/markdown-writer.tests.js | 8 |
2 files changed, 24 insertions, 2 deletions
diff --git a/lib/writers/markdown-writer.js b/lib/writers/markdown-writer.js index 9bafc98..ff2d4b4 100644 --- a/lib/writers/markdown-writer.js +++ b/lib/writers/markdown-writer.js @@ -26,6 +26,8 @@ function markdownLink() { this.close = function() { return "](" + (href || "") + ")"; }; + + this.anchorPosition = "before"; } function markdownImage() { @@ -109,8 +111,15 @@ function markdownWriter() { function open(tagName, attributes) { attributes = attributes || {}; - + + var idHasBeenWritten = false; + if (htmlmarkdown[tagName]) { + if (htmlmarkdown[tagName].anchorPosition === "before") { + writeAnchor(attributes); + idHasBeenWritten = true; + } + if (tagName === "li") { attributes.indent = list.indent; attributes.type = list.type; @@ -129,7 +138,12 @@ function markdownWriter() { list.count = 0; } } - + if (!idHasBeenWritten) { + writeAnchor(attributes); + } + } + + function writeAnchor(attributes) { if (attributes.id) { fragments.push('<a id="' + attributes.id + '"></a>'); } diff --git a/tests/writers/markdown-writer.tests.js b/tests/writers/markdown-writer.tests.js index 7912acf..250bb0c 100644 --- a/tests/writers/markdown-writer.tests.js +++ b/tests/writers/markdown-writer.tests.js @@ -91,6 +91,14 @@ describe('markdown-writer', function() { return assert.equal(writer.asString(), '# <a id="start"></a>Hello\n\n'); }); + test('links have anchors before opening square bracket', function() { + var writer = mdWriter.writer(); + writer.open("a", {href: "http://example.com", id: "start"}); + writer.text("Hello"); + writer.close("a"); + return assert.equal(writer.asString(), '<a id="start"></a>[Hello](http://example.com)'); + }); + test('can generate images', function() { var writer = mdWriter.writer(); writer.selfClosing("img", { "src": "http://example.com/image.jpg", "alt": "Alt Text" }); |
