diff --git a/lib/bitclust/mdcompiler.rb b/lib/bitclust/mdcompiler.rb index 4cec201..cb99991 100644 --- a/lib/bitclust/mdcompiler.rb +++ b/lib/bitclust/mdcompiler.rb @@ -49,7 +49,9 @@ def signature_re INDENTED_FENCE_RE = /\A([ \t]+)(`{3,})/ # dd 段落の継続行(インデント行。ただしインデントフェンスの手前で止まる) DD_TEXT_RE = /\A[ \t](?![ \t]*`{3})/ - DLIST_RE = /\A- \*\*(.+?)\*\*:(?:\s|$)/ + # 用語の後ろに {#id} があればアンカー id として扱う(用語集の各用語への + # リンク用。rurema/doctree#2634)。id は $2 に入る。 + DLIST_RE = /\A- \*\*(.+?)\*\*:(?:[ \t]*\{#([\w-]+)\})?(?:\s|$)/ INFO_RE = /\A- \*\*(?:param|arg|return|raise)\*\*/ SEE_RE = /\A- \*\*SEE\*\*/ # @undef など変換器が生のまま渡す未知メタデータ @@ -271,12 +273,13 @@ def dlist # GFM モード: `term` のコードスパンは として描画し、 # 中身の参照はその中で解決する(...。spec/eval) term = ($1 || raise) + id = $2 # {#id} があれば dt にアンカーを付ける(term の再マッチ前に退避) if gfm? && term =~ /\A`(.+)`\z/ inner = ($1 || raise).strip - line dt("#{rd_compile_text(MarkdownToRRD.restore_inline(inner))}") + line dt("#{rd_compile_text(MarkdownToRRD.restore_inline(inner))}", id) else term = strip_code_span(term).strip - line dt(compile_text(term)) + line dt(compile_text(term), id) end inline = l.sub(DLIST_RE, '').strip @f.ungets(" #{inline}\n") unless inline.empty? diff --git a/lib/bitclust/rdcompiler.rb b/lib/bitclust/rdcompiler.rb index eba7182..1d47d83 100644 --- a/lib/bitclust/rdcompiler.rb +++ b/lib/bitclust/rdcompiler.rb @@ -273,8 +273,8 @@ def dd_without_p line '' end - def dt(s) - "
#{s}
" + def dt(s, id = nil) + id ? %Q(
#{s}
) : "
#{s}
" end def stop_on_syntax_error? diff --git a/sig/bitclust/rdcompiler.rbs b/sig/bitclust/rdcompiler.rbs index 406f7c3..750dd83 100644 --- a/sig/bitclust/rdcompiler.rbs +++ b/sig/bitclust/rdcompiler.rbs @@ -81,7 +81,7 @@ module BitClust # empty lines do not separate paragraphs. def dd_without_p: () -> void - def dt: (String s) -> String + def dt: (String s, ?String? id) -> String METHOD_ATTRIBUTE_LINE_RE: ::Regexp diff --git a/test/test_mdcompiler.rb b/test/test_mdcompiler.rb index 9edd9e7..cb7752a 100644 --- a/test/test_mdcompiler.rb +++ b/test/test_mdcompiler.rb @@ -158,6 +158,18 @@ def test_dlist RD end + def test_dlist_term_anchor + # 用語の後ろの {#id} は dt のアンカー id になる(用語集の各用語リンク用, rurema/doctree#2634) + html = @md.compile("- **アリティー**: {#arity}\n- **`arity`**:\n 仮引数の数。\n") + assert_include(html, '
アリティー
') + # {#id} が無い項目には id は付かない + assert_not_include(html, '
arity') + # 通常の dlist は従来どおり id 無し + plain = @md.compile("- **foo**:\n bar\n") + assert_include(plain, '
foo
') + assert_not_include(plain, '
Integer