Skip to content
This repository was archived by the owner on Jun 27, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions lib/linkify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var regex_text = "# Pirated from http:\/\/daringfireball.net\/2010\/07\/improved_regex_for_matching_urls\r\n# FIXME the first line from there doesn\'t work in JS, can it be omitted? just doing it for now\r\n\\b\r\n( # Capture 1: entire matched URL\r\n (?:\r\n [a-z][\\w-]+: # URL protocol and colon\r\n (?:\r\n \/{1,3} # 1-3 slashes\r\n | # or\r\n [a-z0-9%] # Single letter or digit or \'%\'\r\n # (Trying not to match e.g. \"URI::Escape\")\r\n )\r\n | # or\r\n www\\d{0,3}[.] # \"www.\", \"www1.\", \"www2.\" \u2026 \"www999.\"\r\n | # or\r\n [a-z0-9.\\-]+[.][a-z]{2,4}\/ # looks like domain name followed by a slash\r\n )\r\n (?: # One or more:\r\n [^\\s()<>]+ # Run of non-space, non-()<>\r\n | # or\r\n \\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\) # balanced parens, up to 2 levels\r\n )+\r\n (?: # End with:\r\n \\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\) # balanced parens, up to 2 levels\r\n | # or\r\n [^\\s`!()\\[\\]{};:\'\".,<>?\u00AB\u00BB\u201C\u201D\u2018\u2019] # not a space or one of these punct chars\r\n )\r\n)";

var MAGIC_REGEX = (function() {
return new RegExp(regex_text
.split('\r\n')
.map(function(line) {
return line.split('#')[0].trim()
})
.join('')
,
'gi'
)
})()

function replaceURLs(text, fn) {
if (typeof fn === 'string') {
if (fn === 'html')
fn = function(match, url) {
url = url.replace(/"/g, '&quot;')
return '<a href="'+url+'">'+match+'</a>'
}
else if (fn === 'latex')
fn = function(match, url) {
url = url.replace(/\\/g, '\\\\').replace(/"/g, '\\"')
return '\\href{'+url+'}{'+match+'}'
}
else
throw new Error('unknown replacer type')
}
return text.replace(MAGIC_REGEX, function(match) {
var matchURL = match
if (!/^[a-zA-Z]{1,6}:/.test(matchURL)) matchURL = 'http://' + matchURL
return fn(match, matchURL)
})
}
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
"content_scripts": [
{
"matches": [ "https://workflowy.com/*" ],
"js": [ "lib/jquery-2.1.4.min.js", "lib/jszip.min.js", "lib/jszip-utils.min.js", "lib/FileSaver.min.js", "lib/async.min.js", "src/htmlGenerator.js", "src/converter.js", "src/outline.js", "src/utilities.js" ]
"js": [ "lib/jquery-2.1.4.min.js", "lib/jszip.min.js", "lib/jszip-utils.min.js", "lib/FileSaver.min.js", "lib/async.min.js", "lib/linkify.js", "src/htmlGenerator.js", "src/converter.js", "src/outline.js", "src/utilities.js"]
}
],
"web_accessible_resources": [
"resources/*"
]
}
}
2 changes: 1 addition & 1 deletion src/htmlGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
return className.substring(1);
}));
});
var heading = stripText($(node).attr('text'));
var heading = replaceURLs(stripText($(node).attr('text')), 'html');
var childContent = $.makeArray($(node).children()).map(function (outline) {
return placeholder(outline, headingLevel + 1);
}).join('');
Expand Down