decode &s

This commit is contained in:
crptm 2017-05-30 15:18:59 +04:00
parent ec4212ae8f
commit b7ffa458b0

View File

@ -4,19 +4,28 @@ import React from 'react';
const BOLD_REGEXP = /(\*\*)(.*?)\1/;
const LINK_REGEXP = /\[([^\[]+)\]\(([^\)]+)\)/;
function decodeHTMLEntities(text) {
var entities = [['amp', '&'], ['lt', '<'], ['gt', '>']];
for (var i = 0, max = entities.length; i < max; ++i)
text = text.replace(new RegExp('&' + entities[i][0] + ';', 'g'), entities[i][1]);
return text;
}
function linkify(mdString: string) {
const parts = mdString.split(LINK_REGEXP);
if (parts.length === 1) {
return parts[0];
return decodeHTMLEntities(parts[0]);
}
const result = [];
let i = 0;
while (i + 1 < parts.length) {
result.push(parts[i]);
result.push(decodeHTMLEntities(parts[i]));
result.push(<a href={parts[i + 2]} target="_blank">{parts[i + 1]}</a>);
i += 3;
}
result.push(parts[parts.length - 1]);
result.push(decodeHTMLEntities(parts[parts.length - 1]));
return result.filter(Boolean);
}