Skip to content

Add ability to generate stable heading id for non english languages by setting myst_fully_normalize_name_slug_func #1019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions myst_parser/config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,20 @@ def __repr__(self) -> str:
},
)

fully_normalize_name_slug_func: Callable[[str], str] | None = dc.field(
default=None,
metadata={
"validator": check_heading_slug_func,
"help": (
"Function for normalizing text to valid html id value, "
"or a python import path e.g. `my_package.my_module.my_function` "
"It can be used to transliterate any language to valid stable html identifiers"
),
"global_only": True,
"doc_type": "None | Callable[[str], str] | str",
},
)

html_meta: dict[str, str] = dc.field(
default_factory=dict,
metadata={
Expand Down
6 changes: 5 additions & 1 deletion myst_parser/mdit_to_docutils/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,11 @@ def generate_heading_target(
# during ref resolution, and is not stored in the document.
# TODO this is purely to mimic docutils, but maybe we don't need it?
# (since we have the slugify logic below)
name = nodes.fully_normalize_name(implicit_text)

if self.md_config.fully_normalize_name_slug_func is not None:
name = self.md_config.fully_normalize_name_slug_func(implicit_text)
else:
name = nodes.fully_normalize_name(implicit_text)
node["names"].append(name)
self.document.note_implicit_target(node, node)

Expand Down
5 changes: 4 additions & 1 deletion myst_parser/parsers/docutils_.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ def _attr_to_optparse_option(at: Field, default: Any) -> tuple[dict[str, Any], s
"metavar": "<boolean>",
"validator": frontend.validate_boolean,
}, str(default)
if at.type is str or at.name == "heading_slug_func":
if at.type is str or at.name in (
"heading_slug_func",
"fully_normalize_name_slug_func",
):
return {
"metavar": "<str>",
}, f"(default: '{default}')"
Expand Down