Skip to content

Commit

Permalink
✨ NEW: Add inline_definitions option (#187)
Browse files Browse the repository at this point in the history
This option allows for `definition` token to be inserted into the token stream, at the point where the definition is located in the source text
(in addition to the standard extraction to `env`).
It is not currently part of the Markdown-It JS implementation,
but is useful for cases where one wishes to capture a "loseless"
syntax tree of the parsed Markdown
(in conjunction with the `store_labels` option).
  • Loading branch information
chrisjsewell authored Jan 24, 2022
1 parent 448ea83 commit 99cde43
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 1 deletion.
11 changes: 11 additions & 0 deletions markdown_it/rules_block/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,17 @@ def reference(state: StateBlock, startLine, _endLine, silent):

state.line = startLine + lines + 1

# note, this is not part of markdown-it JS, but is useful for renderers
if state.md.options.get("inline_definitions", False):
token = state.push("definition", "", 0)
token.meta = {
"id": label,
"title": title,
"url": href,
"label": string[1:labelEnd],
}
token.map = [startLine, state.line]

if label not in state.env["references"]:
state.env["references"][label] = {
"title": title,
Expand Down
8 changes: 8 additions & 0 deletions tests/test_port/test_references.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,11 @@ def test_store_labels(data_regression):
src = "[a]\n\n![a]\n\n[a]: ijk"
tokens = md.parse(src)
data_regression.check([token.as_dict() for token in tokens])


def test_inline_definitions(data_regression):
md = MarkdownIt()
md.options["inline_definitions"] = True
src = '[a]: url "title"\n- [a]: url "title"'
tokens = md.parse(src)
data_regression.check([token.as_dict() for token in tokens])
94 changes: 94 additions & 0 deletions tests/test_port/test_references/test_inline_definitions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
- attrs: null
block: true
children: null
content: ''
hidden: false
info: ''
level: 0
map:
- 0
- 1
markup: ''
meta:
id: A
label: a
title: title
url: url
nesting: 0
tag: ''
type: definition
- attrs: null
block: true
children: null
content: ''
hidden: false
info: ''
level: 0
map:
- 1
- 2
markup: '-'
meta: {}
nesting: 1
tag: ul
type: bullet_list_open
- attrs: null
block: true
children: null
content: ''
hidden: false
info: ''
level: 1
map:
- 1
- 2
markup: '-'
meta: {}
nesting: 1
tag: li
type: list_item_open
- attrs: null
block: true
children: null
content: ''
hidden: false
info: ''
level: 2
map:
- 1
- 2
markup: ''
meta:
id: A
label: a
title: title
url: url
nesting: 0
tag: ''
type: definition
- attrs: null
block: true
children: null
content: ''
hidden: false
info: ''
level: 1
map: null
markup: '-'
meta: {}
nesting: -1
tag: li
type: list_item_close
- attrs: null
block: true
children: null
content: ''
hidden: false
info: ''
level: 0
map: null
markup: '-'
meta: {}
nesting: -1
tag: ul
type: bullet_list_close
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ usedevelop = true
extras =
linkify
testing
commands = pytest tests/ {posargs}
commands = pytest {posargs:tests/}

[testenv:py{36,37,38,39,310}-plugins]
extras = testing
Expand Down

0 comments on commit 99cde43

Please sign in to comment.