使用过滤器很容易实现这一点。
这里是linkTable.hs
。一个添加链接表格到文档末尾的过滤器。
import Text.Pandoc.JSON
import Text.Pandoc.Walk
import Data.Monoid
main :: IO ()
main = toJSONFilter appendLinkTable
appendLinkTable :: Pandoc -> Pandoc
appendLinkTable (Pandoc m bs) = Pandoc m (bs ++ linkTable bs)
linkTable :: [Block] -> [Block]
linkTable p = [Header 2 ("linkTable", [], []) [Str "Links"] , Para links]
where
links = concatMap makeRow $ query getLink p
getLink (Link txt (url, _)) = [(url,txt)]
getLink _ = []
makeRow (url, txt) = txt ++ [Str ":", Space, Link [Str url] (url, ""), LineBreak]
使用ghc linkTable.hs
编译过滤器。输出结果如下。
> ghc linkTable.hs
[1 of 1] Compiling Main ( linkTable.hs, linkTable.o )
Linking linkTable ...
> cat example.md
Title
-----
[Python][] is cool!
[Pip] is a package manager.
...
[Python]: http://python.org
[Pip]: https://pip.readthedocs.org
然后使用筛选器运行pandoc
。
> pandoc -t markdown --filter=./linkTable example.md
Title
-----
[Python](http://python.org) is cool!
[Pip](https://pip.readthedocs.org) is a package manager.
...
Links {#linkTable}
-----
Python: <http://python.org>\
Pip: <https://pip.readthedocs.org>\