MarkdownをCosense記法に置換するUserScript
見出しのサイズがかなり大きい
script.js scrapbox.PopupMenu.addButton({
title: 'md→cs',
onClick: text => {
//// リスト ////
// リストのマーカーが行頭にある場合
text = text.split(/\n/).map(line => line.replace(/^[\*\-\+] /g, ' ')).join('\n');
// 行頭以外にあるリストのマーカー(*, -, +)を消す
text = text.split(/\n/).map(line => line.replace(/[\*\-\+] /g, '')).join('\n');
// リストのインデントが半角スペース2個ずつの場合
// text = text.split(/\n/).map(line => line.replace(/ {2}/g, ' ')).join('\n');
// リストのインデントが半角スペース4個ずつの場合
text = text.split(/\n/).map(line => line.replace(/ {4}/g, ' ')).join('\n');
//// 数字付きリスト ////
text = text.split(/\n/).map(line => line.replace(/^([0-9]+\. )/g, ' $1')).join('\n');
//// 強調 ////
// 太字
text = text.split(/\n/).map(line => line.replace(/\*{2}([^*]+)\*{2}/g, '[* $1]')).join('\n');
text = text.split(/\n/).map(line => line.replace(/\_{2}([^_]+)\_{2}/g, '[* $1]')).join('\n');
// イタリック(URLにアンダーバーを含むハイパーリンクの置換を妨害するのでコメントアウト)
// text = text.split(/\n/).map(line => line.replace(/\_([^_]+)\_/g, '[/ $1]')).join('\n');
//// 取り消し線 ////
text = text.split(/\n/).map(line => line.replace(/\~\~(\~+|[^~]+)\~\~/g, '[- $1]')).join('\n');
//// Header level 1-6 ////
text = text.split(/\n/).map(line => line.replace(/^# (.*)/g, '[******* $1]')).join('\n');
text = text.split(/\n/).map(line => line.replace(/^#{2} (.*)/g, '[****** $1]')).join('\n');
text = text.split(/\n/).map(line => line.replace(/^#{3} (.*)/g, '[***** $1]')).join('\n');
text = text.split(/\n/).map(line => line.replace(/^#{4} (.*)/g, '[**** $1]')).join('\n');
text = text.split(/\n/).map(line => line.replace(/^#{5} (.*)/g, '[*** $1]')).join('\n');
text = text.split(/\n/).map(line => line.replace(/^#{6} (.*)/g, '[** $1]')).join('\n');
//// リンクと画像 ////
// Hyperlink without linktext / Image without alt text
text = text.split(/\n/).map(line => line.replace(/!?\[\]\((https?:\/\/[\w/:%#\$&\?\(\)~\.=\+\-]+)\)/g, '[$1]')).join('\n');
// Image with alt text
text = text.split(/\n/).map(line => line.replace(/!\[([^\]]+)\]\((https?:\/\/[\w/:%#\$&\?\(\)~\.=\+\-]+)\)/g, '[$1 $2]')).join('\n');
// Hyperlink with linktext
text = text.split(/\n/).map(line => line.replace(/\[([^\]]+)\]\((https?:\/\/[\w/:%#\$&\?\(\)~\.=\+\-]+)\)/g, '[$1 $2]')).join('\n');
// URL with angle brackets
text = text.split(/\n/).map(line => line.replace(/<(https?:\/\/[\w/:%#\$&\?\(\)~\.=\+\-]+)>/g, '[$1]')).join('\n');
//// 水平線 ////
text = text.split(/\n/).map(line => line.replace(/^([\*|\-|\_]){3,}/g, '[/icons/hr.icon]')).join('\n');
//// エスケープを元に戻す ////
text = text.split(/\n/).map(line => line.replace(/\\([\\|`|\*|_|\{|\}|\[|\]|\(|\)|#|\+|\-|\.|\!])/g, '$1')).join('\n');
return text;
}
});