2017-05-25 22:05:11 +00:00
|
|
|
// ad-hoc parser for translation strings
|
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
const BOLD_REGEXP = /(\*\*)(.*?)\1/;
|
|
|
|
const LINK_REGEXP = /\[([^\[]+)\]\(([^\)]+)\)/;
|
|
|
|
|
2017-05-30 11:18:59 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-05-25 22:05:11 +00:00
|
|
|
function linkify(mdString: string) {
|
|
|
|
const parts = mdString.split(LINK_REGEXP);
|
|
|
|
if (parts.length === 1) {
|
2017-05-30 11:18:59 +00:00
|
|
|
return decodeHTMLEntities(parts[0]);
|
2017-05-25 22:05:11 +00:00
|
|
|
}
|
|
|
|
const result = [];
|
2017-06-01 00:46:03 +00:00
|
|
|
let key = 0;
|
2017-05-25 22:05:11 +00:00
|
|
|
let i = 0;
|
|
|
|
while (i + 1 < parts.length) {
|
2017-05-30 11:18:59 +00:00
|
|
|
result.push(decodeHTMLEntities(parts[i]));
|
2017-06-01 00:46:03 +00:00
|
|
|
result.push(<a key={'linkify-' + key} href={parts[i + 2]} target="_blank">{parts[i + 1]}</a>);
|
|
|
|
key++;
|
2017-05-25 22:05:11 +00:00
|
|
|
i += 3;
|
|
|
|
}
|
2017-05-30 11:18:59 +00:00
|
|
|
result.push(decodeHTMLEntities(parts[parts.length - 1]));
|
2017-05-25 22:05:11 +00:00
|
|
|
return result.filter(Boolean);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function markupToReact(mdString: string) {
|
|
|
|
const parts = mdString.split(BOLD_REGEXP);
|
|
|
|
if (parts.length === 1) {
|
|
|
|
return linkify(parts[0]);
|
|
|
|
}
|
|
|
|
let result = [];
|
2017-06-01 00:46:03 +00:00
|
|
|
let key = 0;
|
2017-05-25 22:05:11 +00:00
|
|
|
let i = 0;
|
|
|
|
while (i + 1 < parts.length) {
|
|
|
|
result = result.concat(linkify(parts[i]));
|
2017-06-01 00:46:03 +00:00
|
|
|
result.push(<b key={'boldify-' + key}>{parts[i + 2]}</b>);
|
|
|
|
key++;
|
2017-05-25 22:05:11 +00:00
|
|
|
i += 3;
|
|
|
|
}
|
|
|
|
result = result.concat(linkify(parts.pop()));
|
|
|
|
return result.filter(Boolean);
|
|
|
|
}
|