From 78c5081a29a3df369c40a76a7c3f80e3dcb082a7 Mon Sep 17 00:00:00 2001 From: gruebait Date: Sun, 20 Dec 2015 15:42:31 -0500 Subject: [PATCH] Support Markdown-driven hyperlinks in MS Word. --- src/utils/html-to-wpml.js | 52 +++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/src/utils/html-to-wpml.js b/src/utils/html-to-wpml.js index 5b95c4c..89f1c51 100644 --- a/src/utils/html-to-wpml.js +++ b/src/utils/html-to-wpml.js @@ -11,49 +11,49 @@ Definition of the Markdown to WordProcessingML conversion routine. module.exports = function( html ) { - var final = ''; - var is_bold = false, is_italic = false, is_link = false; - var depth = 0; - + // Tokenize the HTML stream. var tokens = HTML5Tokenizer.tokenize( html ); + + var final = '', is_bold, is_italic, is_link, link_url; + + // Process , , and elements in the HTML stream, producing + // equivalent WordProcessingML that can be dumped into a or other + // text container element. _.each( tokens, function( tok ) { + switch( tok.type ) { + case 'StartTag': switch( tok.tagName ) { - case 'p': - final += ''; - break; - case 'strong': - is_bold = true; - break; - case 'em': - is_italic = true; - break; + case 'p': final += ''; break; + case 'strong': is_bold = true; break; + case 'em': is_italic = true; break; case 'a': is_link = true; + link_url = tok.attributes.filter(function(attr){ + return attr[0] === 'href'; } + )[0][1]; break; } break; + case 'EndTag': switch( tok.tagName ) { - case 'p': - final += ''; - break; - case 'strong': - is_bold = false; - break; - case 'em': - is_italic = false; - break; - case 'a': - is_link = false; - break; + case 'p': final += ''; break; + case 'strong': is_bold = false; break; + case 'em': is_italic = false; break; + case 'a': is_link = false; break; } break; + case 'Chars': var style = is_bold ? '' : ''; style += is_italic ? '': ''; - final += '' + style + '' + tok.chars + ''; + style += is_link ? '' : ''; + final += + (is_link ? ('') : '') + + '' + style + '' + tok.chars + + '' + (is_link ? '' : ''); break; } });