From af2b4d6860046367ce04c2dad07fb6051f410c78 Mon Sep 17 00:00:00 2001 From: Ben Ma Date: Wed, 10 Jul 2013 15:10:44 -0700 Subject: [PATCH 1/4] Anchor support for id, name added --- lib/formatter.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/formatter.js b/lib/formatter.js index 17bf449..73a482f 100644 --- a/lib/formatter.js +++ b/lib/formatter.js @@ -22,7 +22,11 @@ function formatHeading(elem, fn, options) { } function formatAnchor(elem, fn, options) { - return elem.attribs.href.replace(/^mailto\:/, ''); + var href = ''; + if (elem.attribs.href) { + href += elem.attribs.href.replace(/^mailto\:/, ''); + } + return href; }; function formatHorizontalLine(elem, fn, options) { @@ -135,4 +139,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; From e706ce6df07732792bc33fd31648253bc9ee4bfc Mon Sep 17 00:00:00 2001 From: Ben Ma Date: Wed, 10 Jul 2013 15:54:26 -0700 Subject: [PATCH 2/4] Walk anchor element children --- lib/formatter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/formatter.js b/lib/formatter.js index 73a482f..7557f78 100644 --- a/lib/formatter.js +++ b/lib/formatter.js @@ -26,7 +26,7 @@ function formatAnchor(elem, fn, options) { if (elem.attribs.href) { href += elem.attribs.href.replace(/^mailto\:/, ''); } - return href; + return href + fn(elem.children, options); }; function formatHorizontalLine(elem, fn, options) { From 4548780149711bf3d6f7c324eeca1a1a3971cf02 Mon Sep 17 00:00:00 2001 From: Ben Ma Date: Thu, 11 Jul 2013 16:21:01 -0700 Subject: [PATCH 3/4] Anchor support for placeholders, fragments --- lib/formatter.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/formatter.js b/lib/formatter.js index 7557f78..67e7bb3 100644 --- a/lib/formatter.js +++ b/lib/formatter.js @@ -22,11 +22,24 @@ function formatHeading(elem, fn, options) { } function formatAnchor(elem, fn, options) { - var href = ''; - if (elem.attribs.href) { - href += 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); } - return href + fn(elem.children, options); }; function formatHorizontalLine(elem, fn, options) { From 48669c82501524ff0621d30357153d6abeae90ba Mon Sep 17 00:00:00 2001 From: Ben Ma Date: Thu, 11 Jul 2013 16:21:33 -0700 Subject: [PATCH 4/4] Misc. anchor examples --- example/test.html | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) 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 +