2018-05-23 11:14:38 +00:00
|
|
|
const urlUtils = require('lib/urlUtils');
|
|
|
|
|
2017-08-02 17:47:25 +00:00
|
|
|
const markdownUtils = {
|
|
|
|
|
|
|
|
// Not really escaping because that's not supported by marked.js
|
|
|
|
escapeLinkText(text) {
|
|
|
|
return text.replace(/(\[|\]|\(|\))/g, '_');
|
|
|
|
},
|
|
|
|
|
|
|
|
escapeLinkUrl(url) {
|
|
|
|
url = url.replace(/\(/g, '%28');
|
|
|
|
url = url.replace(/\)/g, '%29');
|
|
|
|
return url;
|
|
|
|
},
|
|
|
|
|
2018-05-23 11:14:38 +00:00
|
|
|
prependBaseUrl(md, baseUrl) {
|
|
|
|
return md.replace(/(\]\()([^\s\)]+)(.*?\))/g, (match, before, url, after) => {
|
|
|
|
return before + urlUtils.prependBaseUrl(url, baseUrl) + after;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-05-23 13:25:59 +00:00
|
|
|
extractImageUrls(md) {
|
|
|
|
// ![some text](http://path/to/image)
|
|
|
|
const regex = new RegExp(/!\[.*?\]\(([^\s\)]+).*?\)/, 'g')
|
|
|
|
let match = regex.exec(md);
|
|
|
|
const output = [];
|
|
|
|
while (match) {
|
|
|
|
const url = match[1];
|
|
|
|
if (output.indexOf(url) < 0) output.push(url);
|
|
|
|
match = regex.exec(md);
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
},
|
|
|
|
|
2017-08-02 17:47:25 +00:00
|
|
|
};
|
|
|
|
|
2018-05-23 11:14:38 +00:00
|
|
|
module.exports = markdownUtils;
|