Skip to content

Commit c6f5192

Browse files
committed
Add option to increase header level for includes
* Increase the header level of included files by 1, by using `include-indented` rather than just `include`. * Add example to test/input.md
1 parent 12dd945 commit c6f5192

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

IncludeFilter.hs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ pandoc command will be executed.
4242
> #do/not/include/this.md
4343
> ```
4444
45+
Alternatively, use the following to increase all the header numbers by one in
46+
the included file.
47+
48+
> ```include-indented
49+
4550
If the file does not exist, it will be skipped completely. No warnings, no
4651
residue, nothing. Putting an # as the first character in the line will make the
4752
filter skip that file.
@@ -60,6 +65,7 @@ import System.Directory
6065
import Text.Pandoc
6166
import Text.Pandoc.Error
6267
import Text.Pandoc.JSON
68+
import Text.Pandoc.Walk
6369

6470
stripPandoc :: Either PandocError Pandoc -> [Block]
6571
stripPandoc p =
@@ -70,28 +76,38 @@ stripPandoc p =
7076
ioReadMarkdown :: String -> IO(Either PandocError Pandoc)
7177
ioReadMarkdown content = return $! readMarkdown def content
7278

73-
getContent :: String -> IO [Block]
74-
getContent file = do
79+
getContent :: Int -> String -> IO [Block]
80+
getContent changeInHeaderLevel file = do
7581
c <- readFile file
7682
p <- ioReadMarkdown c
77-
return $! stripPandoc p
83+
return $! stripPandoc (modifyHeaderLevelWith changeInHeaderLevel <$> p)
7884

7985
getProcessableFileList :: String -> IO [String]
8086
getProcessableFileList list = do
8187
let f = lines list
8288
let files = filter (\x -> not $ "#" `isPrefixOf` x) f
8389
filterM doesFileExist files
8490

85-
processFiles :: [String] -> IO [Block]
86-
processFiles toProcess =
87-
fmap concat (mapM getContent toProcess)
91+
processFiles :: Int -> [String] -> IO [Block]
92+
processFiles changeInHeaderLevel toProcess =
93+
fmap concat (getContent changeInHeaderLevel `mapM` toProcess)
8894

8995
doInclude :: Block -> IO [Block]
9096
doInclude (CodeBlock (_, classes, _) list)
9197
| "include" `elem` classes = do
9298
let toProcess = getProcessableFileList list
93-
processFiles =<< toProcess
99+
processFiles 0 =<< toProcess
100+
| "include-indented" `elem` classes = do
101+
let toProcess = getProcessableFileList list
102+
processFiles 1 =<< toProcess
94103
doInclude x = return [x]
95104

105+
modifyHeaderLevelBlock :: Int -> Block -> Block
106+
modifyHeaderLevelBlock n (Header int att inls) = Header (int + n) att inls
107+
modifyHeaderLevelBlock _ x = x
108+
109+
modifyHeaderLevelWith :: Int -> Pandoc -> Pandoc
110+
modifyHeaderLevelWith n = walk (modifyHeaderLevelBlock n)
111+
96112
main :: IO ()
97113
main = toJSONFilter doInclude

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ pandoc command will be executed.
1616
#do/not/include/this.md
1717
```
1818

19+
Alternatively, use the following to increase all the header numbers by one in
20+
the included file.
21+
22+
```include-indented
23+
1924
If the file does not exist, it will be skipped completely. No warnings, no
2025
residue, nothing. Putting an `#` as the first character in the line will make the
2126
filter skip that file.

test/input.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@ gamma.md
1111
beta.md
1212
```
1313

14+
```include-indented
15+
alpha.md
16+
```
17+
1418
text

0 commit comments

Comments
 (0)