@@ -469,7 +469,7 @@ addBlockId (id', classes, kvs) st =
469469
470470blockTransclusion :: PandocMonad m => MarkdownParser m (F Blocks )
471471blockTransclusion = try $ do
472- guardEnabled Ext_block_transclusion
472+ guardEnabled Ext_wikilink_block_transclusions
473473 char ' !'
474474 res <- wikilink B. linkWith
475475 return $ B. divWith (" " , [" block-transclusion" ], [] ) . B. para <$> res
@@ -2050,20 +2050,34 @@ wikilinkTransclusion = try $ do
20502050 (before, after)
20512051 | titleAfter -> (T. drop 1 after, before)
20522052 | otherwise -> (before, T. drop 1 after)
2053- let (url, blockRef ) = T. break (== ' #' ) target'
2053+ let (url, fragment ) = T. break (== ' #' ) target'
20542054 guard $ T. all (`notElem` [' \n ' ,' \r ' ,' \f ' ,' \t ' ]) url
2055- if T. null blockRef
2055+ if T. null fragment
20562056 then do
2057- guardEnabled Ext_wikilink_transclusion
2057+ guardEnabled Ext_wikilink_transclusions
20582058 currentDir <- takeDirectory . sourceName <$> getPosition
2059- let filename = T. unpack url <> " .md" -- Assume .md extension
2059+ let filename = T. unpack url
2060+ -- Support relative paths like "Folder/File" by using currentDir as base
20602061 insertIncludedFile (fmap B. toInlines <$> parseBlocks) toSources [currentDir] filename Nothing Nothing
20612062 else do
2062- guardEnabled Ext_block_transclusion
2063- currentDir <- takeDirectory . sourceName <$> getPosition
2064- let filename = T. unpack url <> " .md" -- Assume .md extension
2065- let blockId = T. drop 1 blockRef -- Remove the '^' prefix
2066- insertIncludedFile (extractBlockById blockId <$> parseBlocks) toSources [currentDir] filename Nothing Nothing
2063+ let fragmentContent = T. drop 1 fragment -- Remove the '#' prefix
2064+ if T. take 1 fragmentContent == " ^"
2065+ then do
2066+ -- Block ID transclusion: ![[File#^block-id]]
2067+ guardEnabled Ext_wikilink_block_transclusions
2068+ currentDir <- takeDirectory . sourceName <$> getPosition
2069+ let filename = T. unpack url <> " .md" -- Assume .md extension for block transclusion
2070+ let blockId = T. drop 1 fragmentContent -- Remove the '^' prefix
2071+ -- Support relative paths like "Folder/File" by using currentDir as base
2072+ insertIncludedFile (extractBlockById blockId <$> parseBlocks) toSources [currentDir] filename Nothing Nothing
2073+ else do
2074+ -- Heading transclusion: ![[File#Heading]]
2075+ guardEnabled Ext_wikilink_heading_transclusions
2076+ currentDir <- takeDirectory . sourceName <$> getPosition
2077+ let filename = T. unpack url <> " .md" -- Assume .md extension for heading transclusion
2078+ let headingText = fragmentContent
2079+ -- Support relative paths like "Folder/File" by using currentDir as base
2080+ insertIncludedFile (extractHeadingById headingText <$> parseBlocks) toSources [currentDir] filename Nothing Nothing
20672081
20682082note :: PandocMonad m => MarkdownParser m (F Inlines )
20692083note = try $ do
@@ -2391,6 +2405,40 @@ findBlockById targetId = go
23912405 Header _ (bid, _, _) _ | bid == targetId -> Just block
23922406 _ -> go rest
23932407
2408+ -- | Extract content under a specific heading from a list of blocks
2409+ extractHeadingById :: Text -> Blocks -> F Inlines
2410+ extractHeadingById targetHeading blocks =
2411+ case extractContentUnderHeading targetHeading (B. toList blocks) of
2412+ [] -> return mempty
2413+ content -> return $ B. toInlines $ B. fromList content
2414+
2415+ -- | Extract all content under a heading until the next heading of same or higher level
2416+ extractContentUnderHeading :: Text -> [Block ] -> [Block ]
2417+ extractContentUnderHeading targetHeading = go False 0
2418+ where
2419+ go _found _level [] = []
2420+ go found level (block: rest) =
2421+ case block of
2422+ Header lvl _ inlines
2423+ | stringify inlines == targetHeading ->
2424+ -- Found target heading, start collecting content
2425+ block : go True lvl rest
2426+ | found && lvl <= level ->
2427+ -- Found heading of same or higher level, stop collecting
2428+ []
2429+ | found ->
2430+ -- Collecting content under target heading
2431+ block : go True level rest
2432+ | otherwise ->
2433+ -- Haven't found target heading yet
2434+ go False level rest
2435+ _ | found ->
2436+ -- Collecting content under target heading
2437+ block : go True level rest
2438+ | otherwise ->
2439+ -- Haven't found target heading yet
2440+ go False level rest
2441+
23942442blockId :: PandocMonad m => MarkdownParser m (F Inlines )
23952443blockId = try $ do
23962444 guardEnabled Ext_block_ids
0 commit comments