MarkdownをScrapbox記法に置換するUserScript
from xx202import
MarkdownをScrapbox記法に置換するUserScript
MarkdownをScrapbox記法に置換するUserScript - POTLATIPS
code:script.js
scrapbox.PopupMenu.addButton({
title: 'MdSc', //"MdSc" is the name of popup button.
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')
//イタリック
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////
//Header level 1
text=text.split(/\n/).map(line => line.replace(/^# (.*)/g,'$1')).join('\n')
//Header level 2
text=text.split(/\n/).map(line => line.replace(/^#{2} (.*)/g,'$1')).join('\n')
//Header level 3
text=text.split(/\n/).map(line => line.replace(/^#{3} (.*)/g,'$1')).join('\n')
//Header level 4
text=text.split(/\n/).map(line => line.replace(/^#{4} (.*)/g,'$1')).join('\n')
//Header level 5
text=text.split(/\n/).map(line => line.replace(/^#{5} (.*)/g,'$1')).join('\n')
//Header level 6
text=text.split(/\n/).map(line => line.replace(/^#{6} (.*)/g,'$1')).join('\n')
////リンクと画像////
//Hyperlink without linktext / Image without alt text
//e.g. [](https://www.google.com) or ![](https://www.google.com/google.png)
text=text.split(/\n/).map(line => line.replace(/!?\\\((https?:\/\/\w/:%#\$&\?\(\)~\.=\+\-+)\)/g,'$1')).join('\n')
//Image with alt text
//e.g. !Google logo(https://www.google.com/google.png)
text=text.split(/\n/).map(line => line.replace(/!\[(^\]+)\]\((https?:\/\/\w/:%#\$&\?\(\)~\.=\+\-+)\)/g,'$1 $2')).join('\n')
//Hyperlink with linktext
//e.g. Google(https://www.google.com)
text=text.split(/\n/).map(line => line.replace(/\[(^\]+)\]\((https?:\/\/\w/:%#\$&\?\(\)~\.=\+\-+)\)/g,'$1 $2')).join('\n')
//URL with angle brackets
//e.g <https://www.google.com>
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;
}
})