-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add anchor to heading #26
Conversation
when 'tableHeader' | ||
$ 'thead', {key}, [ | ||
$ 'tr', {key: key+'-_inner-tr'}, node.children.map (cell, i) -> | ||
k = key+'-th'+i | ||
$ 'th', {key: k, style: {textAlign: tableAlign[i] ? 'left'}}, toChildren(cell, k) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
toChildren
に defs
渡し忘れかと思って改変しています。
@minodisk md2reactのようなモジュールを要求するようなウェブサイト、多くはSPA環境を想定しているのですが、最近のSPAはハッシュフラグメントをページ状態を表すのに使うことが多く、アンカーによるジャンプは邪魔になる可能性が高いです。 なので、デフォルトオフで無効にするオプションを提供しないと、このパッチを採用するのは難しいと思います。 |
@minodisk |
@mizchi レビューありがとうございます。確かにSPA前提だとすごく邪魔ですね。SPA側でルーティングから除外するような処理書くのもバカバカしいですし。 md2react は今のプレーンなエレメントを吐く。md2react を使う側からオプションでプラガブルに特定のノードタイプの処理をオーバーライトできるようにして、後は勝手にやってくれという設計ってどうでしょうか? md2react raw,
gfm: true
overwrites:
'heading': (node, defs, key) -> # ドメインに依った処理する
@uasi ありがとうございます。別の修正混ぜ込んじゃってごめんなさい 🙇 |
@minodisk すべての子nodeがtoChildren通ってるので、ここでagnosticな関数を通すのはアリですね。 |
今回のケース、 createMdElement = (md) ->
ids = {}
md2react raw,
gfm: true
overwrites:
'heading': (node, defs, key) ->
text = node.children.filter (child) -> child.type is 'text'
.map (child) -> child.value
.join ''
id = text.toLowerCase().replace(/\s/g, '-').replace(/[!<>#%@&='"`:;,\.\*\+\(\)\{\}\[\]\\\/\|\?\^\$]+/g, '')
if !ids[id]?
ids[id] = 0
else
ids[id]++
id = "#{id}-#{ids[id]}"
$ ('h'+node.depth.toString()), {key}, [
$ 'a', {key: key+'-a', id: id, className: 'anchor', href: "##{id}"}, [
$ 'span', {key: key+'-a-span', className: 'icon icon-link'}
]
toChildren(node, defs, key, null)
] |
設計の方針見えたと思うので閉じます。 |
Add anchor node to heading node for internal linking.
before:
after: