diff --git a/RELEASE.md b/RELEASE.md index 436b514..57f7ea7 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -1,8 +1,10 @@ # Pandoc-Filter 0.2.x ## Release 0.2.15 -Working on... - +- Modify `md2html_hash_anchor_and_internal_link_filter` to add an invisible link for each header. + - Now, for each header, an invisible link that links to the header itself will be added to it. + - The added invisible link will be assigned the class of `headerlink`. + - This will be useful for generating anchors by some renderers/frameworks/tools, such as [hexo-theme-butterfly](https://butterfly.js.org/posts/4aa8abbe/?highlight=%25+endnote#%E9%A0%81%E9%9D%A2%E9%8C%A8%E9%BB%9E). ## Release 0.2.14 - Modify `md2html_hash_anchor_and_internal_link_filter` to handle internal links within hexo tag plugins. diff --git a/resources/outputs/test_md2html_header_anchor_and_link.html b/resources/outputs/test_md2html_header_anchor_and_link.html index 578054f..d20ecc0 100644 --- a/resources/outputs/test_md2html_header_anchor_and_link.html +++ b/resources/outputs/test_md2html_header_anchor_and_link.html @@ -237,14 +237,20 @@

test_md2html_anchor_and_link

引用链接

+id="fbf0b5c90527e8a9dd9607effa892937b6395152c108878118104590c3f9e362-1">引用链接

超链接

+id="4a63f752884096b557e2080b0ee0750ed524d5bcf766b68e583b69a670abe7a1-1">超链接

语法:[Link Text](link-address)

效果:Links - Typora Support (typoraio.cn)

参考链接

+id="e33eb0a20e706fad034eff0b75744373919dcde27765bd863f9f215207d675be-1">参考链接

语法:

[Link Text][Ref2] 
@@ -254,7 +260,9 @@ 

test_md2html_anchor_and_link

Link Text

自动链接和网址

+id="bd35eead251dc61c6dfb3ef11792211ea152f91378add9d60a809fa60f57691a-1">自动链接和网址

语法:<http://typora.io>

效果:http://typora.io

@@ -262,7 +270,9 @@

test_md2html_anchor_and_link

效果:This is a link: http://typora.io

本地资源链接

+id="264101a13c49c0740702b6312c1a5bd9f3e9e976089ad711d460ed10529627b0-1">本地资源链接

语法:参考Links - Typora Support (typoraio.cn)

@@ -276,19 +286,28 @@

test_md2html_anchor_and_link

404_page

this_page

页内链接(链接到headings锚点和raw-HTML锚点)

+id="1620841b21c25ccdfa3d8fdd74321f0a85170db8c04330f5b2b372fddb6cabb1-1">页内链接(链接到headings锚点和raw-HTML锚点)

带空格 +id="6865c5c8dc7862478d3c251a684a1a6a09594521713d3c81a91a988932f1cef8-1">带空格 和特殊字符 链接 用于%%%%¥¥¥¥跳转测试 空格

aAa-b -cC Dd, a#%&xxx Zzz [xx] (yy)

+id="4ed057d8cb9bb706509ec393bbef1c95cb64b853ad08c35b6c7a5c3ae43a4206-1">aAa-b cC +Dd, a#%&xxx Zzz [xx] (yy)

留白测试跳转

留白测试跳转

留白测试跳转

@@ -350,17 +375,29 @@

test_md2html_anchor_and_link

  • 链接允许多重#号

    一级目录

    +id="a64ef18ab49bdc3ddeb76c73ef9a93c20a8edfc0737ebb4f11017c46672ff21b-1">一级目录

    二级目录

    +id="d5335a84faaada3830bdd6b4cb3068c010d5c6f266d214a0ef9a7571bea8daad-1">二级目录

    三级目录

    +id="646dd7daa0ffcf85d48e0b11bac57bb541226d8391a1ccff3136fcce78d48d6f-1">三级目录
    四级目录
    +id="4f4ceee94c6d268a19c37bae4a43edc691b00675bf3dae1f60a56a4e43d2c00e-1">四级目录
    五级目录
    +id="a831cf5aa06ec2c00e1d4823da9bea585a61792000a0e399f636e58791a469c3-1">五级目录
    六级目录
    +id="e4a73c34f1669424113083143c44b4924fbe2c0ba8e4acddc3216592fbfda653-1">六级目录

    跳转到一级目录,且是单个#号,#一级目录

    test_md2html_anchor_and_link

  • 需要确保上述匹配过程,至少有一个锚点是存在的。若这样的锚点不存在,渲染器手动抛出异常提醒。
  • hello

    +id="2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824-1">
    hello

    二级目录 hello

    hello

    +id="2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824-2">hello

    一级目录 hello

    hello-2

    +id="f6ddc1bf7d9ef5b2a8d41329728d9c0c3a7a88a59413e8c282204ad4b111d1d1-1">hello-2

    一级目录 hello

    hello

    +id="2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824-3">hello

    三级目录 hello

    hello
    +id="2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824-4">hello

    五级目录 hello

    hello

    +id="2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824-5">hello

    三级目录 hello

    hello

    自定义锚点 hello

    diff --git a/src/pandoc_filter/filters/md2html/hash_anchor_and_internal_link.py b/src/pandoc_filter/filters/md2html/hash_anchor_and_internal_link.py index 4e3cebf..fda54bb 100644 --- a/src/pandoc_filter/filters/md2html/hash_anchor_and_internal_link.py +++ b/src/pandoc_filter/filters/md2html/hash_anchor_and_internal_link.py @@ -77,6 +77,10 @@ def _hash_anchor_id(elem:pf.Element,doc:pf.Doc,tracing_logger:TracingLogger,**kw header_text = pf.convert_text(elem,input_format='panflute',output_format='gfm',standalone=True).lstrip('#') text_hash = __text_hash_count(doc,header_text) elem.identifier = f"{text_hash}-{doc.runtime_dict['anchor_count'][text_hash]}" + # According to https://github.com/Zhaopudark/pandoc-filter/issues/3, add an invisible link in the header. + link_in_hearder = pf.Link(url=f"#{elem.identifier}",title=header_text,classes=['headerlink']) + elem.content.insert(0,link_in_hearder) + tracing_logger.check_and_log('headings anchor',elem) elif isinstance(elem, pf.RawInline) and elem.format == 'html' and (raw_id_text:=get_html_id(elem.text)): # 获取id文本内容但不做任何剔除 tracing_logger.mark(elem) @@ -103,10 +107,12 @@ def _internal_link_recorder(elem:pf.Element,doc:pf.Doc,**kwargs)->None: [modify nothing] """ if isinstance(elem, pf.Link) and elem.url.startswith('#'): - # Olny md internal links need to be decoded since it will be encoded by pandoc before filter. - decoded_url = decode_internal_link_url(elem.url) - url,guessed_url_with_num = __url_hash_guess(decoded_url) - doc.runtime_dict['internal_link_record'].append(InternalLink(elem,url=url,guessed_url=guessed_url_with_num)) + # Olny md internal links need to be decoded since it will be encoded by pandoc before filter.: + if 'headerlink' not in elem.classes: # pass the headerlink + decoded_url = decode_internal_link_url(elem.url) + url,guessed_url_with_num = __url_hash_guess(decoded_url) + doc.runtime_dict['internal_link_record'].append(InternalLink(elem,url=url,guessed_url=guessed_url_with_num)) + elif isinstance(elem, pf.RawInline) and elem.format == 'html' and (old_href:=get_html_href(elem.text)) and old_href.startswith('#'): # raw-HTML internal links will not be encoded by pandoc before filter. So there is no need to decode it. url,guessed_url_with_num = __url_hash_guess(old_href) @@ -124,7 +130,7 @@ def _finalize_hash_anchor_and_internal_link(doc:pf.Doc,tracing_logger:TracingLog elif internal_link.guessed_url in id_set: # None is not in id_set internal_link.sub(f"{internal_link.guessed_url}",tracing_logger) else: - # According https://github.com/Zhaopudark/pandoc-filter/issues/1, + # According to https://github.com/Zhaopudark/pandoc-filter/issues/1, # Even though the internal link's target is not found, we still modify it compulsorily, instead of do nothing. # The warning message is just for reminding the user. tracing_logger.warning("hash_anchor_and_internal_link",f"{internal_link.elem}")