いい感じにリンクするomakase-links
/daiiz/omakase-links
/daiiz/いい感じにリンク化するPopup Menu
自動でリンクを貼るUserScript
https://gyazo.com/f525cf298b48127b7c1d69b15c9b7605
/Icons2/LGTM.icon*3 めちゃくちゃ便利です!!!
手動でリンクを外す仕組みもほしいですねtakker.icon
別のUserScriptで作る
作った
/customize/リンクを外すUserScript
もしくは、リンク入り文章を選ぶとリンクを外すメニューに切り替わるとか
APIの代わりにscrapbox.Project.pagesを使うバージョン
code:script.js
// based on https://scrapbox.io/daiiz/omakase-links
// 既に記法になっているなどの理由で、置換すべきでない範囲を取得する
const detectLocked = text => {
const syntax = /\[(\/\w|[^\!"#%&'()\*\+,\-\.\/\{\|\}<>_~)[^\\]*\]/g;
const locked = text.split('').map(_ => false)
code:script.js
const matches = text.matchAll(syntax);
for (const match of matches) {
locked.fill(true, match.index, match.index + match0.length);
}
return locked;
}
from 正規表現 - JavaScript | MDN
code:script.js
const escapeRegExp = string =>string.replace(/[.*+?^=!:${}()|\\/\\]/g, '\\$&');
const autoLink = text => {
if (!text) return;
// リンクを長い順に並べる
const titles = scrapbox.Project.pages.map(p => p.title).sort((a, b) => b.length - a.length);
const locked = detectLocked(text)
const matched = text.split('').map(_ => null)
const allNull = arr => arr.every(x => x === null);
for (const title of titles) {
const regexp = new RegExp(escapeRegExp(title), 'gi');
const matches = text.matchAll(regexp);
for (const match of matches) {
const len = match0.length;
const idx = match.index;
if (allNull(matched.slice(idx, idx + len)) && !lockedidx) {
for (let i = 0; i < len; i++) {
let c = textidx + i;
if (i === 0) c = [${c};
if (i === len - 1) c = ${c}];
matchedidx + i = c;
}
}
}
}
// 無駄にテロメアがupdateされるのを防ぐ為、何も置換しない時は何も返さない
if (allNull(matched)) return
// 配列matchedでnullの位置を、元のtextの文字に置き換える
return matched.map((c, idx) => c ?? textidx).join('')
}
scrapbox.PopupMenu.addButton({
title: '✨',
onClick: autoLink
})
#UserScript