Skip to content

Commit

Permalink
fix: add better slugging (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
agoose77 committed Aug 20, 2024
1 parent d32be83 commit 7732408
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/sphinx_ext_mystmd/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import hashlib

from .transform import MySTNodeVisitor
from .utils import to_text, find_by_type, breadth_first_walk
from .utils import to_text, find_by_type, breadth_first_walk, title_to_name


logger = logging.getLogger(__name__)
Expand All @@ -17,7 +17,8 @@ class MySTBuilder(Builder):
name = "myst"

def _slugify(self, path):
return path.replace("/", "-")
name = os.path.basename(path)
return title_to_name(name)

def _get_xref_path(self, doc_name):
target_stem = self._slugify(doc_name)
Expand Down
2 changes: 1 addition & 1 deletion src/sphinx_ext_mystmd/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sphinx.util import logging


from .utils import normalize_label
from .utils import normalize_label


logger = logging.getLogger(__name__)
Expand Down
17 changes: 17 additions & 0 deletions src/sphinx_ext_mystmd/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@
import collections


def title_to_name(title):
return input_to_name(title.replace("&", "¶and¶"), re.compile(r"[a-z0-9-]"), "-")[:50]


def input_to_name(input_, allowed, join):
escaped = ''.join([c if allowed.search(c) else '¶' for c in f"¶{input_}".lower()])
unique = re.sub(r"¶+", "¶", escaped)
name = re.sub("¶", join, unique[1:])
if join:
name = re.sub(f"{join}+", join, name)
if name[0] == join:
name = name[1:]
if name[-1] == join:
name = name[:-1]
return name


def normalize_label(
label,
):
Expand Down

0 comments on commit 7732408

Please sign in to comment.