Scrapboxの検索窓をHelpfeel的に使う

Scrapboxの検索窓をHelpfeel的に使う

(2022/10/27)

ScrapboxだけでHelpfeel的検索ができるような工夫をしている
Helpfeel記述のあるページにおいて、Helpfeel記述を展開したタイトルのページを作って同じ内容を貼っている
タイトルの先頭に「_」をつけることで、自動生成したものであることを示している
そういうページはリストには表示されないようにしている (settingsで設定)
style.css
Copied!
.grid-style-item[data-page-title^="_"]
{display: none !important;}

以下のような手順
/masuiのデータを masui.json にエクスポートしておく
% wget -q -O - "https://scrapbox.io/api/code/masui/Scrapboxの検索窓をHelpfeel的に使う/expand.rb | ruby - masui > new.json
new.jsonをインポート
こうしておくと、「Appple」みたいな間違ったキーワードでもQuickSearchが有効に働く

以下のようなsbexpandコマンドを作るとラク
sbexpand
Copied!
#!/bin/sh
#
# masui.json のあるところで以下を実行
# % sbexpand masui
#
wget -q -O - "https://scrapbox.io/api/code/masui/Scrapboxの検索窓をHelpfeel的に使う/expand.rb" | ruby - $*

この場合の手順は以下のとおり
プロジェクトのデータを プロジェクト名.json にエクスポート
sbexpand プロジェクト名 > new.json
new.jsonをインポート (全部上書き)

expand.rb
Copied!
require 'json'
require 're_expand'

project = ARGV[0] || "toshiyukimasui"
project = project.sub(/\.json$/,'')

outdata = {}
pages = []
outdata['pages'] = pages

data = JSON.parse(File.read("#{project}.json"))

glossary = {}
data['pages'].each { |page|
if page['title'] == 'Glossary'
page['lines'].each { |line|
if line =~ /(.*):\s*(.*)$/
glossary[$1] = $2
end
}
end
}
data['pages'].each { |page|
title = page['title']
helps = []
normallines = []
expanded = false
page['lines'].each { |line|
if line =~ /expanded from/
expanded = true
end
if line =~ /^\s*\?\s+(.*)$/
s = $1
while s =~ /\{([a-zA-Z]+)\}/
kw = $1
if glossary[kw]
s.sub!("{#{kw}}",glossary[kw])
end
end
helps << s
else
normallines << line
end
}

normallines.shift # タイトル行を削除
if helps.length > 0
helps.each { |help|
help.gsub(/\[/,'').gsub(/\]/,'').expand { |s|
name = s[0]
if name != "" && name != title
entry = {}
entry['title'] = "_#{name}"
entry['lines'] = [
"#{name}",
"(expanded from [#{title}] [[このページは編集しないでください]])",
""
]
normallines.each { |normalline|
entry['lines'].push(normalline)
}
pages.push(entry)
end
}
}
# pages.push(page)
else
if !expanded
# pages.push(page)
end
end
}

puts outdata.to_json


Powered by Helpfeel