Skip to content

Commit

Permalink
Added retention of pre-formatted links
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-p committed Feb 17, 2013
1 parent 0be37a8 commit a13073c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/common/markdown-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@
.replace(/<\/p\b[^>]*>/ig, '</div>');
}

// Some tags we can convert to Markdown
preprocessInfo.html = convertHTMLtoMarkdown('a', preprocessInfo.html);

// Experimentation has shown some tags that need to be tweaked a little.
preprocessInfo.html =
preprocessInfo.html
Expand All @@ -140,6 +143,10 @@
// that attribute.
// If `ifNotHasString` is non-null, tags that contain that string will not
// be matched. Note that `ifNotHasString` will be used in a regex.
// TODO: This function is pretty flawed. Wrapping block elements in paras
// doesn't make much sense. And if we're going to support inline
// elements, then we can't unconditionally put linebreaks around the
// the wrapped elements.
function excludeTagBlocks(tagName, preprocessInfo, wrapInPara, ifHasAttribute, ifNotHasString) {
var depth, startIndex, openIndex, closeIndex, currentOpenIndex,
openTagRegex, closeTagRegex, remainder, closeTagLength, regexFiller;
Expand Down Expand Up @@ -253,6 +260,22 @@
}
}

/**
Converts instances of `tag` in `html` to Markdown and returns the
resulting HTML.
*/
function convertHTMLtoMarkdown(tag, html) {
if (tag === 'a') {
html = html.replace(/<a\b[^>]+href="([^"]*)"[^>]*>(.*?)<\/a>/ig, '[$2]($1)');
}
else {
throw new Error('convertHTMLtoMarkdown: ' + tag + ' is not a supported tag');
return;
}

return html;
}

// Restore the content that we extracted so that it wouldn't be processed.
function postprocessHtml(renderedMarkdown, preprocessInfo) {
var i;
Expand Down Expand Up @@ -294,6 +317,10 @@
return codeElem.innerHTML;
}

markdownRender._testExports = {
convertHTMLtoMarkdown: convertHTMLtoMarkdown
};

var EXPORTED_SYMBOLS = ['markdownRender'];

if (typeof module !== 'undefined') {
Expand Down
2 changes: 2 additions & 0 deletions src/common/test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<div id="mocha"></div>

<script src="../utils.js"></script>
<script src="../markdown-render.js"></script>
<script src="../options-store.js"></script>

<script src="../vendor/jquery-1.9.1.js"></script>
Expand All @@ -31,6 +32,7 @@
<script src="test-prep.js"></script>
<script src="options-store-test.js"></script>
<script src="utils-test.js"></script>
<script src="markdown-render-test.js"></script>
<script src="test-run.js"></script>

</body>
Expand Down
37 changes: 37 additions & 0 deletions src/common/test/markdown-render-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright Adam Pritchard 2013
* MIT License : http://adampritchard.mit-license.org/
*/


describe('Markdown-Render', function() {
it('should exist', function() {
expect(markdownRender).to.exist;
});

describe('convertHTMLtoMarkdown', function() {
var convertHTMLtoMarkdown = markdownRender._testExports.convertHTMLtoMarkdown;

it('should throw an exception for unsupported tags', function() {
expect(_.partial(convertHTMLtoMarkdown, 'badtag')).to.throw(Error);
});

it('should not modify the string if there is no match', function() {
var s = 'aaa <b>bbb</b> ccc <div>ddd</div> eee';
expect(convertHTMLtoMarkdown('a', s)).to.equal(s);
});

it('should replace the given tag', function() {
var s, target;

s = 'aaa <b>bbb</b> ccc <div>ddd</div> eee <a href="fff">ggg</a> hhh';
target = 'aaa <b>bbb</b> ccc <div>ddd</div> eee [ggg](fff) hhh';
expect(convertHTMLtoMarkdown('a', s)).to.equal(target);

s = 'aaa <b>bbb</b> ccc <div>ddd</div> eee <a href="fff">ggg</a> hhh <a href="iii">jjj <em>kkk</em></a> lll';
target = 'aaa <b>bbb</b> ccc <div>ddd</div> eee [ggg](fff) hhh [jjj <em>kkk</em>](iii) lll';
expect(convertHTMLtoMarkdown('a', s)).to.equal(target);
});

});
});

0 comments on commit a13073c

Please sign in to comment.