@@ -82,26 +82,33 @@ import Text.Pandoc.Walk
82
82
83
83
stripPandoc :: Int -> Either PandocError Pandoc -> [Block ]
84
84
stripPandoc _ (Left _) = [Null ]
85
- stripPandoc changeInHeaderLevel (Right (Pandoc meta blocks)) = maybe id (:) ( title meta) $ modBlocks
85
+ stripPandoc changeInHeaderLevel (Right (Pandoc meta blocks)) = maybe id (:) title modBlocks
86
86
where
87
- modBlocks = modifyHeaderLevelBlockWith changeInHeaderLevel <$> blocks
88
- title (Meta (Map. lookup " title" -> Just (MetaInlines inls))) = do
87
+ getTitle (Meta (Map. lookup " title" -> Just (MetaInlines inls))) = Just inls
88
+ getTitle _ = Nothing
89
+ theTitle = getTitle meta
90
+ modBlocks = modifyHeaderLevelBlockWith changeInHeaderLevel (titleRef <$> theTitle) <$> blocks
91
+ title = do
92
+ inls <- theTitle
89
93
guard $ changeInHeaderLevel > 0
90
94
Just $ Header changeInHeaderLevel (titleRef inls,[" section-title" ],[] ) inls
91
- title _ = Nothing
92
95
-- WARNING titleRef doesn't check that titles are unique; for that try uniqueIdent.
93
96
titleRef = stringify . fmap (lowerCase . dashFromSpace)
94
97
dashFromSpace Space = Str " -"
95
98
dashFromSpace x = x
96
99
lowerCase (Str x) = Str (fmap C. toLower x)
97
100
lowerCase x = x
98
101
99
- modifyHeaderLevelBlockWith :: Int -> Block -> Block
100
- modifyHeaderLevelBlockWith n (Header int att inls) = Header (int + n) att inls
101
- modifyHeaderLevelBlockWith _ x = x
102
+ amendIdentifier :: String -> Attr -> Attr
103
+ amendIdentifier title (ident, cls, kvs) = (concat [title, " -" , ident], cls, kvs)
104
+
105
+ modifyHeaderLevelBlockWith :: Int -> Maybe String -> Block -> Block
106
+ modifyHeaderLevelBlockWith n mtitle (Header int att inls) =
107
+ Header (int + n) (maybe id amendIdentifier mtitle att) inls
108
+ modifyHeaderLevelBlockWith _ _ x = x
102
109
103
110
modifyHeaderLevelWith :: Int -> Pandoc -> Pandoc
104
- modifyHeaderLevelWith n = walk (modifyHeaderLevelBlockWith n)
111
+ modifyHeaderLevelWith n = walk (modifyHeaderLevelBlockWith n mempty )
105
112
106
113
ioReadMarkdown :: String -> IO (Either PandocError Pandoc )
107
114
ioReadMarkdown content = return $! readMarkdown def content
0 commit comments