format-text
import
code:script.js
import { isASCII } from '../utils/script.js'
import { findNodes } from '../node/URLNode.js'
import { parse } from '/api/code/takker/scrapbox-parser@7.1.0/script.js'
const 箇条書き先頭をスペースにする = (文字列) => {
return 文字列.replace(/^\s*/g, (s) => s.replace(/\s/g, ' '))
}
const 全角スペースを半角にする = (文字列) => {
return 文字列.replace(/ /g, ' ')
}
const 全角英数字を半角にする = (文字列) => {
String.fromCharCode(s.charCodeAt(0) - 0xfee0)
)
}
const 適切な丸括弧にする = (文字列) => {
const 半角丸括弧にする = (文字列) => {
return 文字列.replaceAll('(', '(').replaceAll(')', ')')
}
const 対象文字列 = 半角丸括弧にする(文字列)
const 半角丸括弧で括ってるパターン = /(?<leftBracket>\s*\()(?<content>.*?)(?<rightBracket>\)\s*)/g
const 出力文字列要素 = []
let カーソル位置 = 0
for (const match of 対象文字列.matchAll(半角丸括弧で括ってるパターン)) {
出力文字列要素.push(対象文字列.slice(カーソル位置, match.index))
カーソル位置 = match.index + match0.length const 丸括弧内の文字列がASCIIのみで構成される = isASCII(
match.groups.content
)
if (丸括弧内の文字列がASCIIのみで構成される) {
出力文字列要素.push( (${match.groups.content}) )
} else {
出力文字列要素.push((${match.groups.content}))
}
}
出力文字列要素.push(対象文字列.slice(カーソル位置))
return 出力文字列要素.join('').trimEnd()
}
const Scrapboxの絶対リンクをScrapboxリンク表記にする = (文字列) => {
const nodes = parse(文字列, { hasTitle: false })
const urlNodes = []
const findAbsoluteScrapboxURLNodes = findNodes((node) => {
return (
node.type === 'link' &&
node.pathType === 'absolute' &&
/^https?:\/\/scrapbox\.io\//.test(node.href)
)
})
findAbsoluteScrapboxURLNodes(nodes, urlNodes)
urlNodes.forEach((urlNode) => {
const 置換後文字列 =
[/${projectName} +
(pageName == null || pageName === '' ? '' : /${decodeURI(pageName)}) +
']'
文字列 = 文字列.replace(urlNode.raw, 置換後文字列)
})
return 文字列
}
const 絶対リンクをURL_TITLEの順にする = (文字列) => {
const nodes = parse(文字列, { hasTitle: false })
const urlNodes = []
const findAbsoluteURLNodes = findNodes((node) => {
console.log(node)
return node.type === 'link' && node.pathType === 'absolute'
})
findAbsoluteURLNodes(nodes, urlNodes)
urlNodes.forEach((urlNode) => {
const 置換後文字列 = [${urlNode.href} ${urlNode.content}]
文字列 = 文字列.replace(urlNode.raw, 置換後文字列)
})
return 文字列
}
export const format = (選択文字列) => {
const 変換後文字列 = 選択文字列
.split('\n')
.map(箇条書き先頭をスペースにする)
.map(全角英数字を半角にする)
.map(全角スペースを半角にする)
.map(適切な丸括弧にする)
.map(Scrapboxの絶対リンクをScrapboxリンク表記にする)
.map(絶対リンクをURL_TITLEの順にする)
.join('\n')
if (選択文字列 === 変換後文字列) {
return
}
return 変換後文字列
}