nora
code:script.js
scrapbox.PopupMenu.addButton({
title: 'DC',
onClick: text => {
const dateElm = document.querySelector(".dropdown-menu-right .created span.date-label");
text = text.replace(/\[/g, '').replace(/\]/g, ''); // []だと処理済みリンクと区別できなくなるため
const arr = [
{ // title urlになっているリンクを修正する
regexp: /\[\[(^\]+) ((h?)(ttps?:\/\/a-zA-Z0-9.\-_@:/~?%&;=+#',()*!+))\]\]/g,
func(_match, title, url, _h, _href) { return [${title}](${url}) },
},
{ //url title
regexp: /\[\[((h?)(ttps?:\/\/a-zA-Z0-9.\-_@:/~?%&;=+#',()*!+)) (^\]+)\]\]/g,
func(_match, url, _h, _href, title) { return [${title}](${url}) },
},
{ // gyazo
regexp: /\[\[((h?)(ttps?:\/\/gyazo.com\/a-zA-Z0-9.\-_@:/~?%&;=+#',()*!+))\]\]/g,
func(_match, url, _h, _href) { return ![](${url}/raw) },
},
{ // 太字のみ
regexp: /\[\(\*+)\s(\S+)\\]/g,
func(_match, strong, text) { return <b data-bold="${strong.length}">${text}</b> }
},
{ // 太字以外を含む文字修飾
regexp: /\[\[(!"#%&'()*+,-./{|}<>_~+)\s(\S+)\]\]/g,
func(_match, deco, text) { return <span data-deco="${deco}">${text}</span> }
},
{ // ![]()にダブルブラケット
regexp: /\[\[(!\[\S*\(^)*\))\]\]/g,
func(_match, link) { return link }
},
{ //画像url→![]()
regexp: /\[\[((h?)(ttps?:\/\/a-zA-Z0-9.\-_@:/~?%&;=+#',()*!+))(\.(jpg|jpeg|png|bmp|gif|JPG|JPEG))\]\]/g,
func(_match, url, _h, _href, ex) { return ![](${url}${ex}) },
},
{ //https://www.youtube.com/~→ブラケットを外す
regexp: /\[\[((h?)(ttps?:\/\/www\.youtube\.com\/a-zA-Z0-9.\-_@:/~?%&;=+#',()*!+))\]\]/g,
func(_match, url, _h, _href) { return url },
},
{
regexp: /\[\.*?(\.icon)\\]/g,
func(_match) { return "" }
},
{
regexp: /\[\(.*?)\\]/g,
func(_match, text) { return text }
}
]
for (const obj of arr) {
text = text.replace(obj.regexp, obj.func);
}
// const replaced = dateElm.title + "\n\t" + text.split("\n").map(str => str.replace(/^\s+/, match => "\t".repeat(match.length))).map(str => str || " ").join("\n\t");
const replaced = text.split("\n").map(str => str.replace(/^\s+/, match => "\t".repeat(match.length))).map(str => str || " ").join("\n");
// const replaced = text.replace(/^\s+/, match => "\t".repeat(match.length)); // 行の先頭のスペースをタブに置換
navigator.clipboard.writeText(replaced)
.then(() => {
const display = (num) => text.length < num ? text : text.slice(0, num) + '…';
console.group('Copied to clipboard');
console.log(display(100));
console.groupEnd();
})
.catch(err => {
console.log('Something went wrong', err);
})
}
});