Markdown reader: fenced code block shortcuts with attributes (#8174)

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.
This commit is contained in:
Siphalor
2022-08-25 19:18:11 +02:00
committed by GitHub
parent 201e86d9aa
commit 8670f6dc5b
3 changed files with 63 additions and 3 deletions
+12
View File
@@ -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
+7 -3
View File
@@ -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)
+44
View File
@@ -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" ] ]
````