feeles-ide
Version:
The hackable and serializable IDE to make learning material
36 lines (29 loc) • 936 B
JavaScript
/**
* HTML に書き出しても問題ないようなテキストに変換する
* - 全体をコメントアウト
* - コメントのサニタイズ
* e.g. "<!-- {TEXT} -->" to "--><!-- {TEXT} --><!--"
*/
var commentRegExp = /<!--[^!]*-->/g;
export function encode(text) {
// 内容に HTML コメントが含まれている
if (commentRegExp.test(text)) {
text = text.replace(commentRegExp, function (hit) {
console.log(hit);
return '-->' + hit + '<!--';
});
}
return '<!--\n' + text + '\n-->';
}
var sanitizedRegExp = /--><!--([^!]*)--><!--/g;
export function decode(text) {
// コメントを外す
text = text.replace(/^\s*<!--\n?/, '').replace(/\n?-->\s*$/, '');
// 内容に HTML コメントが含まれていた
if (sanitizedRegExp.test(text)) {
text = text.replace(sanitizedRegExp, function (hit, inner) {
return '<!--' + inner + '-->';
});
}
return text;
}