Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Riccardo Odone
Riccardo Odone

Posted on • Edited on • Originally published atodone.me

     

Custom Markdown in Pandoc

You can keep reading here orjump to my blog to get the full experience, including the wonderful pink, blue and white palette.


The architecture of Pandoc is well described in thedocumentation:

Pandoc consists of a set of readers and writers. When converting a document from one format to another, text is parsed by a reader into pandoc’s intermediate representation of the document—an “abstract syntax tree” or AST—which is then converted by the writer into the target format.

For example, let's take a Markdown file:

``haskellx= 1``
Enter fullscreen modeExit fullscreen mode

Pandoc translates it to the following AST:

stackinstallpandocstackexec-- pandoc-s-f markdown-t native file.markdown# Pandoc#   (Meta {unMeta = fromList []})#   [CodeBlock ("",["haskell"],[]) "x = 1"]
Enter fullscreen modeExit fullscreen mode

CodeBlock is one of the value constructors of theBlock type:

dataBlock=CodeBlockAttrText--  | ...typeAttr=(Text,[Text],[(Text,Text)])--           ^ Id--                 ^ Classes--                         ^ Key-Value pairs
Enter fullscreen modeExit fullscreen mode

Pandoc allows changing the AST before the output document is written.

One way to achieve that is by using a filter:

#!/usr/bin/envrunghc{-# LANGUAGE OverloadedStrings #-}importText.PandocimportText.Pandoc.JSONmain::IO()main=toJSONFiltertransformtransform::Block->Blocktransform(CodeBlockattrcontent)=CodeBlockattr"y = 2"transformx=x
Enter fullscreen modeExit fullscreen mode

I'm usingrunghc in this case to make sure the version of Pandoc used in the filter is the same as the one invoked on the command line:

stackexec-- pandoc-s-f markdown-t native--filter filter.hs file.markdown# Pandoc#   (Meta {unMeta = fromList []})#   [CodeBlock ("",["haskell"],[]) "y = 2"]
Enter fullscreen modeExit fullscreen mode

Notice that the code changed fromx = 1 toy = 2.

The same can be achieved in pure Haskell code:

{-# LANGUAGE OverloadedStrings #-}importData.TextimportText.PandocimportText.Pandoc.JSONimportText.Pandoc.Walkmain::IO()main=dofile<-readFile"./file.markdown"result<-runIO$dodoc<-readMarkdown(def{readerExtensions=pandocExtensions})(packfile)lettransformed=walktransformdocwriteNative(def{writerExtensions=pandocExtensions})transformedhandleErrorresult>>=putStrLn.unpacktransform::Block->Blocktransform(CodeBlockattrcontent)=CodeBlockattr"y = 2"transformx=x
Enter fullscreen modeExit fullscreen mode
stack runghc pure-haskell.hs# [CodeBlock ("",["haskell"],[]) "y = 2"]
Enter fullscreen modeExit fullscreen mode

With Hakyll, similar transformations can be achieved by usingpandocCompilerWithTransform orpandocCompilerWithTransformM.

That is how tweetable pull quotes areimplemented on this blog. In particular, the following Markdown

``pullquoteIf we are not open to silly ideas, then why even bother with a creative activity in the first place?``In a recent workshop I attended one rule was "all ideas are brilliant". Yes, at first an idea could be raw, maybe even silly. However, by thinking outside the box, it may turn into something innovative. More often than not though, it will be a draw in the blank. Still, if we are not open to silly ideas, then why even bother with a creative activity in the first place? In fact, if you always do what you've always done, you'll always get what you've always got.
Enter fullscreen modeExit fullscreen mode

becomes

Screenshot of a pull quote from a blog post


If you want to dig deeper, thePandoc documentation on filters is a great read on the topic.


Get the latest content via email from me personally. Reply with your thoughts. Let's learn from each other. Subscribe to myPinkLetter!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Location
    Kraków
  • Work
    Software Maverick
  • Joined

More fromRiccardo Odone

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp