Skip to content

Commit

Permalink
Markdown: Link reference definitions (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
stkb committed Nov 29, 2021
1 parent e74892c commit 69da796
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
37 changes: 36 additions & 1 deletion core/Parsers_Markdown.fs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,41 @@ let private footnote : TryNewParser =
}


/// Link reference definition. "Wraps" a single paragraph
let linkRefDef : TryNewParser =
fun ctx ->
let rxLabel = mdMarker @"\[\s*\S.*?\]:\s*"

let rec wrapFLR : FirstLineRes -> FirstLineRes = function
| Pending r -> Pending (wrapResultParser nlpWrapper r)
| Finished r -> Finished (wrapResultParser (fun _ -> Some flpWrapper) r)

and flpWrapper line : FirstLineRes =
match tryMatch rxLabel line with
| Some m -> onMatch line m
| None ->
match tryParsers paraTerminators ctx line with
| Some flr -> flr
| None -> defaultPara ctx line |> wrapFLR

and nlpWrapper paraParser line : NextLineRes =
match tryMatch rxLabel line with
| Some m -> FinishedOnPrev <| Some (onMatch line m )
| None ->
match tryParsers paraTerminators ctx line with
| None ->
match paraParser line with
| ThisLine flr -> ThisLine (wrapFLR flr)
| FinishedOnPrev r -> FinishedOnPrev r
| someParser -> FinishedOnPrev someParser

and onMatch line (m: string[]) : FirstLineRes =
let prefixFn = blankOut' line.split (indentWidth line) 0 " "
defaultPara ctx (trimWhitespace line) |> wrapFLR |> wrapPrefixFn prefixFn

fun line -> tryMatch rxLabel line <|>> onMatch line


/// List item container
let private listItem : TryNewParser =
fun ctx ->
Expand Down Expand Up @@ -192,7 +227,7 @@ let private paraTerminators : List<TryNewParser> =

/// list of all blocks except default paragraph
let private contentBlocks : List<TryNewParser> =
paraTerminators @ [indentedCode]
paraTerminators @ [linkRefDef; indentedCode]


/// Finds a new block, checking all options and falling back on default paragraph in
Expand Down
51 changes: 51 additions & 0 deletions docs/content-types/markdown/linkrefdefs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Markdown: Link reference definitions

> language: "markdown"
This doesn't precisely follow the [link reference definition
specification](https://spec.commonmark.org/current/#link-reference-definitions), but
should be a "good enough" implementation

A link reference definition, like all markdown paragraphs, can be indented up to 3 spaces.
It has the pattern `[<label>]:` where `<label>` is characters containing at least 1
non-whitespace character.

If a single long line is wrapped, created lines are indented 4 spaces, to match
[footnotes](footnotes.md).

[link]: /url "link title" -> [link]: /url ¦
¦ "link title"¦


Link reference definitions do **not** interrupt a paragraph; ie. there must be a blank
line between one and a normal paragraph. If there's no blank line it'll be treated as part
of the paragraph.

paragraph ¦ paragraph text ¦
text [link] ¦ -> [link] [link]: ¦
[link]: some_url ¦ some_url ¦


LDRs consist of just a single paragraph, and no blank line is required between multiple
LRDs.

text [one] ¦ text [one] text ¦
text [two] ¦ [two] ¦
¦ -> ¦
[one]: url1 description [one]: url1 ¦
[two]: url2 ¦ description ¦
[two]: url2 ¦


LRDs are terminated by any of the normal paragraph-interrupting blocks, like fenced code
block or list item.

[one]: ¦ [one]: url¦
url ¦ - list ¦
- list item item ¦
¦ ¦
[two]: ¦ -> [two]: url¦
url ¦ ``` js ¦
``` js ¦ let i; ¦
let i; ¦ ``` ¦
``` ¦ ¦

0 comments on commit 69da796

Please sign in to comment.