Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,28 @@ def inline(match):
def slug(heading):
"""A heading's anchor as GitHub generates it: from the rendered text, so
link targets, HTML tags and emphasis markers drop out, while underscores
inside words stay."""
text = re.sub(r"!?\[([^\]]*)\]\([^)]*\)", r"\1", heading.strip())
text = re.sub(r"<[^>]+>", "", text)
inside words stay. A code span keeps its text as written."""

# Set each code span aside behind a placeholder, so the rules below see a
# link or emphasis that wraps code whole but never touch the code itself.
code = []

def hold(match):
code.append(match.group(2))
return f"\x00{len(code) - 1}\x00"

# An escaped backtick is text, not a code fence. A code span opens and
# closes with backtick runs of the same length.
text = heading.strip().replace("\\`", "\x01")
text = re.sub(r"(?<!`)(`+)(?!`)(.+?)(?<!`)\1(?!`)", hold, text)
text = re.sub(r"!?\[([^\]]*)\]\([^)]*\)", r"\1", text)
text = re.sub(r"<!--.*?-->", "", text)
# Only real tags: a letter or "/" right after "<", so a comparison such as
# "a < b and c > d" is text, not a tag. A tag goes whole, attributes and
# any backticks inside them included.
text = re.sub(r"</?[A-Za-z][A-Za-z0-9-]*(?:\s[^<>]*)?/?>", "", text)
text = re.sub(r"(?<![\w_])(_{1,2})(?=\S)(.+?)(?<=\S)\1(?![\w_])", r"\2", text)
text = re.sub(r"\x00(\d+)\x00", lambda m: code[int(m.group(1))], text)
return re.sub(r"[^\w\- ]", "", text.lower()).replace(" ", "-")


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,21 @@ MD
[[ "$output" == *"open-questions.md#oq-2 release"* ]]
}

@test "a Blocks target matches GitHub's anchor for a heading with inline code" {
# GitHub keeps a code span's text as written: angle brackets and underscores
# inside code are not HTML tags or emphasis.
sed -i.bak 's/^## Invitation extension$/## Call `__init__` with `Promise<Result>` and _care_/' "$B/roadmap.md"
sed -i.bak 's/roadmap.md#invitation-extension/roadmap.md#call-__init__-with-promiseresult-and-care/' "$B/open-questions.md"
rm "$B/roadmap.md.bak" "$B/open-questions.md.bak"
run "$OQ" index "$B"
[ "$status" -eq 0 ] || { echo "status $status: $output" >&2; return 1; }
run "$OQ" check "$B"
[ "$status" -eq 0 ] || { echo "status $status: $output" >&2; return 1; }
run "$OQ" blocking "$B" roadmap.md#call-__init__-with-promiseresult-and-care --level release
[ "$status" -eq 1 ]
[[ "$output" == *"open-questions.md#oq-2 release"* ]]
}

@test "a Blocks target naming a missing document fails the check" {
sed -i.bak 's/| roadmap.md#invitation-extension |/| rollout-plan.md#invitation-extension |/' "$B/open-questions.md"
rm "$B/open-questions.md.bak"
Expand Down Expand Up @@ -978,3 +993,39 @@ expect_refused() {
refute_grep -qF "skip features/FEATURE/" <<< "$text"
done
}

@test "heading anchors match GitHub's for common heading shapes" {
run python3 -B - "$OQ" <<'PY'
import importlib.util, sys
spec = importlib.util.spec_from_file_location("oq", sys.argv[1])
oq = importlib.util.module_from_spec(spec)
spec.loader.exec_module(oq)
cases = {
"Plain heading": "plain-heading",
"_em_ and __strong__": "em-and-strong",
"*em* and **strong**": "em-and-strong",
"[Docs](https://example.com)": "docs",
"snake_case": "snake_case",
"<kbd>Enter</kbd>": "enter",
"Call `__init__` with `Promise<Result>`": "call-__init__-with-promiseresult",
"``Promise<Result>``": "promiseresult",
"What if `count` < `min` or `count` > `max`?": "what-if-count--min-or-count--max",
"Keep a < b and c > d": "keep-a--b-and-c--d",
"A <span class=\"x\">tagged</span> word": "a-tagged-word",
"Run <span title=\"`x`\">now</span>": "run-now",
"Call _`run`_": "call-run",
"Call [`run`](guide.md)": "call-run",
"Step <!-- note --> one": "step--one",
"Escaped \\`tick\\` stays": "escaped-tick-stays",
"Run ``a `b` c`` now": "run-a-b-c-now",
"Odd `a`` runs": "odd-a-runs",
"Use [`npm install`](https://example.com)": "use-npm-install",
"Use _the `cache` key_": "use-the-cache-key",
"Use __the `cache` key__": "use-the-cache-key",
}
bad = [(h, oq.slug(h), want) for h, want in cases.items() if oq.slug(h) != want]
print(bad)
sys.exit(1 if bad else 0)
PY
[ "$status" -eq 0 ] || { echo "$output" >&2; return 1; }
}