diff --git a/example/test.html b/example/test.html index 5745f52..69dc81d 100644 --- a/example/test.html +++ b/example/test.html @@ -118,6 +118,28 @@

Mailto formating

+ + +
+

Misc. anchor examples

+
+ +

name attribute superceded by id in HTML5

+
+
+
+ A placeholder hyperlink +
+
+ +

For more information, visit example.com

+
+
+
+ Ignore fragment href +
+ + - \ No newline at end of file + diff --git a/lib/formatter.js b/lib/formatter.js index 17bf449..67e7bb3 100644 --- a/lib/formatter.js +++ b/lib/formatter.js @@ -22,7 +22,24 @@ function formatHeading(elem, fn, options) { } function formatAnchor(elem, fn, options) { - return elem.attribs.href.replace(/^mailto\:/, ''); + if (elem.attribs && elem.attribs.href) { + var mailto = /^mailto\:/i; + if (mailto.test(elem.attribs.href)) { + // Keep address + return elem.attribs.href.replace(mailto, ''); + } else { + var frag = /^#/; + if (frag.test(elem.attribs.href)) { + // Ignore fragment + return fn(elem.children, options); + } else { + // Typical link + return elem.attribs.href + fn(elem.children, options); + } + } + } else { + return fn(elem.children, options); + } }; function formatHorizontalLine(elem, fn, options) { @@ -135,4 +152,4 @@ exports.table = formatTable; exports.orderedList = formatOrderedList; exports.unorderedList = formatUnorderedList; exports.listItem = formatListItem; -exports.horizontalLine = formatHorizontalLine; \ No newline at end of file +exports.horizontalLine = formatHorizontalLine;