Skip to content

Commit

Permalink
Markdown reader: fenced code block shortcuts with attributes (#8174)
Browse files Browse the repository at this point in the history
This allows the combination of the fenced code block shortcut form with
attributes:

````
```haskell {.class #id}
```
````

The code syntax class will be combined with the attribute classes.

This syntax allows for more intuitive writing and for better compatibility
with other Markdown parsers such as GitHub or Codeberg.

Closes #8174.
  • Loading branch information
Siphalor authored Aug 25, 2022
1 parent 201e86d commit 8670f6d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
12 changes: 12 additions & 0 deletions MANUAL.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3857,6 +3857,18 @@ This is equivalent to:
qsort [] = []
```

This shortcut form may be combined with attributes:

```haskell {.numberLines}
qsort [] = []
```

Which is equivalent to:

``` {.haskell .numberLines}
qsort [] = []
```

If the `fenced_code_attributes` extension is disabled, but
input contains class attribute(s) for the code block, the first
class attribute will be printed after the opening fence as a bare
Expand Down
10 changes: 7 additions & 3 deletions src/Text/Pandoc/Readers/Markdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -688,9 +688,13 @@ codeBlockFenced = try $ do
rawattr <-
(Left <$> (guardEnabled Ext_raw_attribute >> try rawAttribute))
<|>
(Right <$> option ("",[],[])
((guardEnabled Ext_fenced_code_attributes >> try attributes)
<|> ((\x -> ("",[toLanguageId x],[])) <$> many1Char nonspaceChar)))
(Right <$> (do
languageId <- option Nothing (Just . toLanguageId <$> try (many1Char $ satisfy (\x -> x `notElem` ['`', '{', '}'] && not (isSpace x))))
skipMany spaceChar
maybeAttr <- option Nothing (Just <$> (guardEnabled Ext_fenced_code_attributes >> try attributes))
return $ case maybeAttr of
Nothing -> ("", maybeToList languageId, [])
Just (elementId, classes, attrs) -> (elementId, maybe classes (: classes) languageId, attrs)))
blankline
contents <- T.intercalate "\n" <$>
manyTill (gobbleAtMostSpaces indentLevel >> anyLine)
Expand Down
44 changes: 44 additions & 0 deletions test/command/8174.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
````
% pandoc -t native
```yaml {#id}
some: code
```
^D
[ CodeBlock ( "id" , [ "yaml" ] , [] ) "some: code" ]
````

````
% pandoc -t native
```yaml {.class #id}
some: code
```
^D
[ CodeBlock
( "id" , [ "yaml" , "class" ] , [] ) "some: code"
]
````

<!-- Inline code sections at the start of the line should not be mistaken for code blocks -->
````
% pandoc -t native
```ab```
^D
[ Para [ Code ( "" , [] , [] ) "ab" ] ]
````

````
% pandoc -t native
```ab```{.class}
^D
[ Para [ Code ( "" , [ "class" ] , [] ) "ab" ] ]
````

<!-- Illegal language identifiers should be treated as inline code for now -->
````
% pandoc -t native
``` foo}{.bar}
test
```
^D
[ Para [ Code ( "" , [] , [] ) "foo}{.bar} test" ] ]
````

0 comments on commit 8670f6d

Please sign in to comment.