60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
from __future__ import annotations
|
|
|
|
import re
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Heading:
|
|
level: int
|
|
title: str
|
|
slug: str
|
|
|
|
|
|
def slugify_heading(text: str) -> str:
|
|
slug = text.strip().casefold()
|
|
slug = re.sub(r"[^\w\s\u4e00-\u9fff-]", "", slug)
|
|
slug = re.sub(r"[\s_]+", "-", slug)
|
|
slug = re.sub(r"-{2,}", "-", slug)
|
|
return slug.strip("-")
|
|
|
|
|
|
def heading_tokens(tokens):
|
|
"""Shared title/anchor sequence for rendering, the outline and navigation."""
|
|
seen: dict[str, int] = {}
|
|
for index, token in enumerate(tokens):
|
|
if token.type != 'heading_open' or index + 1 >= len(tokens):
|
|
continue
|
|
inline = tokens[index + 1]
|
|
if inline.type != 'inline':
|
|
continue
|
|
slug = slugify_heading(inline.content)
|
|
if not slug:
|
|
continue
|
|
count = seen.get(slug, 0)
|
|
seen[slug] = count + 1
|
|
if count:
|
|
slug = f'{slug}-{count + 1}'
|
|
yield token, Heading(int(token.tag[1:]), inline.content, slug)
|
|
|
|
|
|
def heading_locations(markdown_text: str, markdown=None):
|
|
if markdown is None:
|
|
# Lazy import avoids loading the renderer during application bootstrap.
|
|
from .markdown_renderer import MarkdownRenderer
|
|
markdown = MarkdownRenderer(Path.cwd()).markdown
|
|
return [(heading, token.map[0]) for token, heading in heading_tokens(markdown.parse(markdown_text, {})) if token.map]
|
|
|
|
|
|
def extract_headings_from_text(markdown_text: str) -> list[Heading]:
|
|
return [heading for heading, _ in heading_locations(markdown_text)]
|
|
|
|
|
|
def extract_headings(path: Path) -> list[Heading]:
|
|
try:
|
|
return extract_headings_from_text(path.read_text(encoding="utf-8"))
|
|
except UnicodeDecodeError:
|
|
return []
|
|
|