Added pandoc filter to parse links in vimwiki

This commit is contained in:
2024-02-24 12:31:53 +01:00
parent 6c18e44033
commit 61e7d14266
4 changed files with 34 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
# How to compile the filter
In order to compile the filter you must type: `ghc -static --make filter.hs`. Keep in mind that you don't need to compile anything, because you can just make the `filter.hs` file executable via `chmod +x filter.hs` and then run it via:
`pandoc -f markdown -t html --filter ./filter.hs -o index.html index.md`.
To do that remember also to add the shebang: `#!/usr/bin/env runhaskell`

10
.config/vimwiki/delink.hs Executable file
View File

@@ -0,0 +1,10 @@
#!/usr/bin/env runhaskell
-- This haskell filter removes links from the document
import Text.Pandoc.JSON
main = toJSONFilter delink
delink :: Inline -> [Inline]
delink (Link _ txt _) = txt
delink x = [x]

BIN
.config/vimwiki/linkParser Executable file

Binary file not shown.

18
.config/vimwiki/linkParser.hs Executable file
View File

@@ -0,0 +1,18 @@
#!/usr/bin/env runhaskell
-- This simple filter checks if the link contained is a valid url, if it isn't
-- it adds a .html extenstion to the link (it's supposed to be a page in the wiki)
import Text.Pandoc.JSON
import Text.Regex.TDFA ((=~))
import Data.Text (pack, unpack)
main = toJSONFilter linkParser
linkParser :: Inline -> Inline
linkParser (Link attr txt (url, title)) = Link attr txt (newUrl, title)
where
newUrl = if isValidURL (unpack url) then url else pack (unpack url ++ ".html")
linkParser x = x
isValidURL :: String -> Bool
isValidURL url = url =~ ("^((https?|ftp)://)*[a-zA-Z0-9]+(\\.[a-zA-Z0-9]+)+([/?].*)?$" :: String)