add local anchors to headers
This commit is contained in:
		
							parent
							
								
									1c214fa9f7
								
							
						
					
					
						commit
						0f97b7a64f
					
				
							
								
								
									
										16
									
								
								Utils.hs
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								Utils.hs
									
									
									
									
									
								
							| 
						 | 
					@ -1,3 +1,5 @@
 | 
				
			||||||
 | 
					{-# LANGUAGE OverloadedStrings #-}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module Utils where
 | 
					module Utils where
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import Control.Monad.IO.Class
 | 
					import Control.Monad.IO.Class
 | 
				
			||||||
| 
						 | 
					@ -41,6 +43,20 @@ walkURLs f = Text.Pandoc.Walk.walkM go
 | 
				
			||||||
      pure $ Image a i (u', t)
 | 
					      pure $ Image a i (u', t)
 | 
				
			||||||
    go x = pure x
 | 
					    go x = pure x
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-- | A pandoc walker for adding the local links to the headings (links are
 | 
				
			||||||
 | 
					-- appended and get a given class)
 | 
				
			||||||
 | 
					addHeadingLinks ::
 | 
				
			||||||
 | 
					     T.Text -> Text.Pandoc.Definition.Pandoc -> Text.Pandoc.Definition.Pandoc
 | 
				
			||||||
 | 
					addHeadingLinks cls = Text.Pandoc.Walk.walk go
 | 
				
			||||||
 | 
					  where
 | 
				
			||||||
 | 
					    go h@(Header lvl attr@(id, _, _) inlines) =
 | 
				
			||||||
 | 
					      Header
 | 
				
			||||||
 | 
					        lvl
 | 
				
			||||||
 | 
					        attr
 | 
				
			||||||
 | 
					        (inlines ++
 | 
				
			||||||
 | 
					         [Link ("", [cls], []) [Str "#"] ("#" <> id, "Link to this section")])
 | 
				
			||||||
 | 
					    go x = x
 | 
				
			||||||
 | 
					
 | 
				
			||||||
hasUriScheme :: String -> String -> Bool
 | 
					hasUriScheme :: String -> String -> Bool
 | 
				
			||||||
hasUriScheme x = all id . zipWith (==) x . (++ ":")
 | 
					hasUriScheme x = all id . zipWith (==) x . (++ ":")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -43,6 +43,17 @@ a:hover {
 | 
				
			||||||
	text-decoration: underline;
 | 
						text-decoration: underline;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					a.header-local-anchor {
 | 
				
			||||||
 | 
						color: #eee;
 | 
				
			||||||
 | 
						font-weight: 300;
 | 
				
			||||||
 | 
						padding-left: 0.5em;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					a.header-local-anchor:hover {
 | 
				
			||||||
 | 
						text-decoration: none;
 | 
				
			||||||
 | 
						color: #0ad;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
body {
 | 
					body {
 | 
				
			||||||
	margin: 0;
 | 
						margin: 0;
 | 
				
			||||||
	background: white;
 | 
						background: white;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										4
									
								
								site.hs
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								site.hs
									
									
									
									
									
								
							| 
						 | 
					@ -178,7 +178,9 @@ installPage mount pi = do
 | 
				
			||||||
  fixedUrlDoc <-
 | 
					  fixedUrlDoc <-
 | 
				
			||||||
    walkURLs (processLink $ pi ^. pagePath . to takeDirectory) $ pi ^. pageDoc
 | 
					    walkURLs (processLink $ pi ^. pagePath . to takeDirectory) $ pi ^. pageDoc
 | 
				
			||||||
  checkTarget file
 | 
					  checkTarget file
 | 
				
			||||||
  body <- io . runIOorExplode $ writeHtml5String htmlWriteOpts fixedUrlDoc
 | 
					  body <-
 | 
				
			||||||
 | 
					    io . runIOorExplode $ writeHtml5String htmlWriteOpts $
 | 
				
			||||||
 | 
					    addHeadingLinks "header-local-anchor" fixedUrlDoc
 | 
				
			||||||
  let Y.Object meta' = pi ^. pageMeta
 | 
					  let Y.Object meta' = pi ^. pageMeta
 | 
				
			||||||
      meta = Y.Object $ KM.insert "body" (Y.String body) meta'
 | 
					      meta = Y.Object $ KM.insert "body" (Y.String body) meta'
 | 
				
			||||||
  meta <- addGlobalMeta meta >>= addPageMeta pi
 | 
					  meta <- addGlobalMeta meta >>= addPageMeta pi
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue