-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds block_args for autogenerated ids from trustworthy sources
- Loading branch information
Showing
3 changed files
with
135 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
pkgs/tools/nix/nixos-render-docs/src/tests/test_auto_id_prefix.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
from pathlib import Path | ||
|
||
from markdown_it.token import Token | ||
from nixos_render_docs.manual import HTMLConverter, HTMLParameters | ||
from nixos_render_docs.md import Converter | ||
|
||
auto_id_prefix="TEST_PREFIX" | ||
def set_prefix(token: Token, ident: str) -> None: | ||
token.attrs["id"] = f"{auto_id_prefix}-{ident}" | ||
|
||
|
||
def test_auto_id_prefix_simple() -> None: | ||
md = HTMLConverter("1.0.0", HTMLParameters("", [], [], 2, 2, 2, Path("")), {}) | ||
|
||
src = f""" | ||
# title | ||
## subtitle | ||
""" | ||
tokens = Converter()._parse(src) | ||
md._handle_headings(tokens, on_heading=set_prefix) | ||
|
||
assert [ | ||
{**token.attrs, "tag": token.tag} | ||
for token in tokens | ||
if token.type == "heading_open" | ||
] == [ | ||
{"id": "TEST_PREFIX-1", "tag": "h1"}, | ||
{"id": "TEST_PREFIX-1.1", "tag": "h2"} | ||
] | ||
|
||
|
||
def test_auto_id_prefix_repeated() -> None: | ||
md = HTMLConverter("1.0.0", HTMLParameters("", [], [], 2, 2, 2, Path("")), {}) | ||
|
||
src = f""" | ||
# title | ||
## subtitle | ||
# title2 | ||
## subtitle2 | ||
""" | ||
tokens = Converter()._parse(src) | ||
md._handle_headings(tokens, on_heading=set_prefix) | ||
|
||
assert [ | ||
{**token.attrs, "tag": token.tag} | ||
for token in tokens | ||
if token.type == "heading_open" | ||
] == [ | ||
{"id": "TEST_PREFIX-1", "tag": "h1"}, | ||
{"id": "TEST_PREFIX-1.1", "tag": "h2"}, | ||
{"id": "TEST_PREFIX-2", "tag": "h1"}, | ||
{"id": "TEST_PREFIX-2.1", "tag": "h2"}, | ||
] | ||
|
||
def test_auto_id_prefix_maximum_nested() -> None: | ||
md = HTMLConverter("1.0.0", HTMLParameters("", [], [], 2, 2, 2, Path("")), {}) | ||
|
||
src = f""" | ||
# h1 | ||
## h2 | ||
### h3 | ||
#### h4 | ||
##### h5 | ||
###### h6 | ||
## h2.2 | ||
""" | ||
tokens = Converter()._parse(src) | ||
md._handle_headings(tokens, on_heading=set_prefix) | ||
|
||
assert [ | ||
{**token.attrs, "tag": token.tag} | ||
for token in tokens | ||
if token.type == "heading_open" | ||
] == [ | ||
{"id": "TEST_PREFIX-1", "tag": "h1"}, | ||
{"id": "TEST_PREFIX-1.1", "tag": "h2"}, | ||
{"id": "TEST_PREFIX-1.1.1", "tag": "h3"}, | ||
{"id": "TEST_PREFIX-1.1.1.1", "tag": "h4"}, | ||
{"id": "TEST_PREFIX-1.1.1.1.1", "tag": "h5"}, | ||
{"id": "TEST_PREFIX-1.1.1.1.1.1", "tag": "h6"}, | ||
{"id": "TEST_PREFIX-1.2", "tag": "h2"}, | ||
] |