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