Skip to content
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
5 changes: 4 additions & 1 deletion src/render/markdown/anchors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ pub(crate) fn resolve_anchors(doc: &Document) -> AnchorMap {
// inside the heading content) to those slugs.
walk_blocks(&doc.blocks, &mut |block| {
if let Block::Heading { anchor, content, .. } = block {
let Some(slug) = ids.claim(gfm_slug(&inlines_to_plain_text(content))) else {
// A line break renders as a space in a heading, and the slug is
// taken from the rendered text.
let text = inlines_to_plain_text(content).replace('\n', " ");
let Some(slug) = ids.claim(gfm_slug(&text)) else {
return;
};
let mut bind = |id: &str| {
Expand Down
18 changes: 18 additions & 0 deletions src/render/markdown/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,24 @@ fn heading_coincident_anchor_uses_slug() {
assert_eq!(md, "## Section Two\n\n[go](#section-two)\n");
}

#[test]
fn heading_line_break_slugs_as_the_space_it_renders() {
// A heading renders a line break as a space, so the slug a link to it
// uses must separate the words the same way.
let md = doc(vec![
Block::Paragraph(vec![Inline::Link {
content: vec![Inline::plain("go")],
target: LinkTarget::Anchor("toc1".into()),
}]),
Block::Heading {
level: 1,
anchor: Some("toc1".into()),
content: vec![Inline::plain("Part One"), Inline::LineBreak, Inline::plain("Overview")],
},
]);
assert_eq!(md, "[go](#part-one-overview)\n\n# Part One Overview\n");
}

#[test]
fn duplicate_heading_slugs_deduped() {
let md = doc(vec![
Expand Down