たとえばこんな感じに。
TOC
もしObsidianに目次用のコールアウトを作るとしたら上記記事を参照してください。
mokuji.md
ただ、上記のままだとはてなブログに使えないので、普通のリンクにしたのが下記スクリプトです。
<%* s = tp.file.content d = s.split("\n") .filter(x => x.match(/^#+\s/)) .map(x => { s = x.replace(/^#+\s/, "") return `>[${s}](#${s})` }).join(" \n") tp.file.cursor_append(`>【目次】 \n${d}`) %>
使い方
単に、文章の先頭で実行するだけ。
バリエーション
もっと単純に見出しだけ抜き出すなら、こうかな。
<%* s = tp.file.content d = s.split("\n") .filter(x => x.match(/^#+\s/)) .map(x => { s = x.replace(/^#+\s/, "") return `>${s}` }).join(" \n") tp.file.cursor_append(`>【目次】 \n${d}`) %>
段差もつけるとしたら、こんな感じで。
<%* s = tp.file.content d = s.split("\n") .filter(x => x.match(/^#+\s/)) .map(x => { s = x.replace(/#\s/, " ") s = s.replace(/#/g, "..") return `>${s}` }).join(" \n") tp.file.cursor_append(`>【目次】 \n${d}`) %>
まとめ
detailsとsummaryで作ろうとしたら、Markdownリンクに対応してなかった。
追記
段差付きリストはこう?
<%* s = tp.file.content d = s.split("\n") .filter(x => x.match(/^#+\s/)) .map(x => { s = x.replace(/#\s/, "- ") s = s.replace(/#/g, " ") return s }).join(" \n") tp.file.cursor_append(`【目次】\n${d}`) %>
半角スペースだとコード表記と解釈するので、全角スペースで段差をつけました。