Skip to content

Commit b2779c9

Browse files
committed
switch to insertIncludedFile
1 parent 9892d00 commit b2779c9

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/Text/Pandoc/Readers/Markdown.hs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,12 +2055,15 @@ wikilinkTransclusion = try $ do
20552055
if T.null blockRef
20562056
then do
20572057
guardEnabled Ext_wikilink_transclusion
2058-
let attr = (mempty, ["wikilink", "transclusion"], [("data-transclusion", "true")])
2059-
return $ return $ B.imageWith attr url "" (B.text $ fromEntities title')
2058+
currentDir <- takeDirectory . sourceName <$> getPosition
2059+
let filename = T.unpack url <> ".md" -- Assume .md extension
2060+
insertIncludedFile (fmap B.toInlines <$> parseBlocks) toSources [currentDir] filename Nothing Nothing
20602061
else do
20612062
guardEnabled Ext_block_transclusion
2062-
let attr = (mempty, ["wikilink", "transclusion"], [("block-ref", T.drop 1 blockRef), ("data-transclusion", "true")])
2063-
return $ return $ B.imageWith attr target' "" (B.text $ fromEntities title')
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
20642067

20652068
note :: PandocMonad m => MarkdownParser m (F Inlines)
20662069
note = try $ do
@@ -2369,6 +2372,25 @@ doubleQuoted = do
23692372
many1Till inline doubleQuoteEnd))
23702373
<|> (return (return (B.str "\8220")))
23712374

2375+
-- | Extract a block with a specific ID from a list of blocks
2376+
extractBlockById :: Text -> Blocks -> F Inlines
2377+
extractBlockById targetId blocks =
2378+
case findBlockById targetId (B.toList blocks) of
2379+
Just block -> return $ B.toInlines $ B.fromList [block]
2380+
Nothing -> return mempty
2381+
2382+
-- | Find a block with a specific ID in a list of blocks
2383+
findBlockById :: Text -> [Block] -> Maybe Block
2384+
findBlockById targetId = go
2385+
where
2386+
go [] = Nothing
2387+
go (block:rest) =
2388+
case block of
2389+
Div (bid, _, _) [Para _] | bid == targetId -> Just block
2390+
Div (bid, _, _) _ | bid == targetId -> Just block
2391+
Header _ (bid, _, _) _ | bid == targetId -> Just block
2392+
_ -> go rest
2393+
23722394
blockId :: PandocMonad m => MarkdownParser m (F Inlines)
23732395
blockId = try $ do
23742396
guardEnabled Ext_block_ids

test/command/obsidian.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,28 @@ A paragraph with a block ID. ^my-id
1818
% pandoc -f obsidian -t native
1919
An embed: ![[myfile.md]]
2020
^D
21-
[ Para [ Str "An" , Space , Str "embed:" , Space , Image ( "" , [ "wikilink" , "transclusion" ] , [ ( "data-transclusion" , "true" ) ] ) [ Str "myfile.md" ] ( "myfile.md" , "" ) ] ]
21+
[ Para [ Str "An" , Space , Str "embed:" , Space ] ]
2222
```
2323

2424
```
2525
% pandoc -f obsidian -t native
2626
A block reference embed: ![[myfile.md#^my-id]]
2727
^D
28-
[ Para [ Str "A" , Space , Str "block" , Space , Str "reference" , Space , Str "embed:" , Space , Image ( "" , [ "wikilink" , "transclusion" ] , [ ( "block-ref" , "my-id" ) , ( "data-transclusion" , "true" ) ] ) [ Str "myfile.md#^my-id" ] ( "myfile.md#^my-id" , "" ) ] ]
28+
[ Para [ Str "A" , Space , Str "block" , Space , Str "reference" , Space , Str "embed:" , Space ] ]
2929
```
3030

3131
```
3232
% pandoc -f obsidian -t native
3333
A wikilink transclusion: ![[my-wikilink]]
3434
^D
35-
[ Para [ Str "A" , Space , Str "wikilink" , Space , Str "transclusion:" , Space , Image ( "" , [ "wikilink" , "transclusion" ] , [ ( "data-transclusion" , "true" ) ] ) [ Str "my-wikilink" ] ( "my-wikilink" , "" ) ] ]
35+
[ Para [ Str "A" , Space , Str "wikilink" , Space , Str "transclusion:" , Space ] ]
3636
```
3737

3838
```
3939
% pandoc -f obsidian -t native
4040
A block transclusion: ![[myfile#^my-id]]
4141
^D
42-
[ Para [ Str "A" , Space , Str "block" , Space , Str "transclusion:" , Space , Image ( "" , [ "wikilink" , "transclusion" ] , [ ( "block-ref" , "my-id" ) , ( "data-transclusion" , "true" ) ] ) [ Str "myfile#^my-id" ] ( "myfile#^my-id" , "" ) ] ]
42+
[ Para [ Str "A" , Space , Str "block" , Space , Str "transclusion:" , Space ] ]
4343
```
4444

4545
```

0 commit comments

Comments
 (0)