@@ -509,9 +509,12 @@ atxHeader = try $ do
509509 notFollowedBy $ guardEnabled Ext_fancy_lists >>
510510 (char '.' <|> char ')') -- this would be a list
511511 skipSpaces
512− text <- trimInlinesF . mconcat <$> many (notFollowedBy atxClosing >> inline)
512+ (text, raw) <- withRaw $
513+ trimInlinesF . mconcat <$> many (notFollowedBy atxClosing >> inline)
513514 attr <- atxClosing
514− attr' <- registerHeader attr (runF text defaultParserState)
515+ attr'@(ident,_,_) <- registerHeader attr (runF text defaultParserState)
516+ guardDisabled Ext_implicit_header_references
517+ <|> registerImplicitHeader raw ident
515518 return $ B.headerWith attr' level <$> text
516519
517520atxClosing :: MarkdownParser Attr
@@ -544,15 +547,24 @@ setextHeader = try $ do
544547 -- This lookahead prevents us from wasting time parsing Inlines
545548 -- unless necessary -- it gives a significant performance boost.
546549 lookAhead $ anyLine >> many1 (oneOf setextHChars) >> blankline
547− text <- trimInlinesF . mconcat <$> many1 (notFollowedBy setextHeaderEnd >> inline)
550+ (text, raw) <- withRaw $
551+ trimInlinesF . mconcat <$> many1 (notFollowedBy setextHeaderEnd >> inline)
548552 attr <- setextHeaderEnd
549553 underlineChar <- oneOf setextHChars
550554 many (char underlineChar)
551555 blanklines
552556 let level = (fromMaybe 0 $ findIndex (== underlineChar) setextHChars) + 1
553− attr' <- registerHeader attr (runF text defaultParserState)
557+ attr'@(ident,_,_) <- registerHeader attr (runF text defaultParserState)
558+ guardDisabled Ext_implicit_header_references
559+ <|> registerImplicitHeader raw ident
554560 return $ B.headerWith attr' level <$> text
555561
562+registerImplicitHeader :: String -> String -> MarkdownParser ()
563+registerImplicitHeader raw ident = do
564+ let key = toKey $ "[" ++ raw ++ "]"
565+ updateState (\s -> s { stateHeaderKeys =
566+ M.insert key ('#':ident,"") (stateHeaderKeys s) })
567+
556568--
557569-- hrule block
558570--
@@ -1700,7 +1712,7 @@ referenceLink :: (String -> String -> Inlines -> Inlines)
17001712 -> (F Inlines, String) -> MarkdownParser (F Inlines)
17011713referenceLink constructor (lab, raw) = do
17021714 sp <- (True <$ lookAhead (char ' ')) <|> return False
1703− (ref,raw') <- option (mempty, "") $
1715+ (_,raw') <- option (mempty, "") $
17041716 lookAhead (try (spnl >> normalCite >> return (mempty, "")))
17051717 <|>
17061718 try (spnl >> reference)
@@ -1720,13 +1732,13 @@ referenceLink constructor (lab, raw) = do
17201732 return $ do
17211733 keys <- asksF stateKeys
17221734 case M.lookup key keys of
1723− Nothing -> do
1724− headers <- asksF stateHeaders
1725− ref' <- if labIsRef then lab else ref
1735+ Nothing ->
17261736 if implicitHeaderRefs
1727− then case M.lookup ref' headers of
1728− Just ident -> constructor ('#':ident) "" <$> lab
1729− Nothing -> makeFallback
1737+ then do
1738+ headerKeys <- asksF stateHeaderKeys
1739+ case M.lookup key headerKeys of
1740+ Just (src, tit) -> constructor src tit <$> lab
1741+ Nothing -> makeFallback
17301742 else makeFallback
17311743 Just (src,tit) -> constructor src tit <$> lab
17321744