Skip to content

Commit

Permalink
fix: prefix anchors in spec links (rust-lang#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
eagr authored Aug 31, 2022
1 parent 4536d6c commit 93c7875
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ fn write_rust<W: std::io::Write>(
section: &Section,
features: &[Feature],
) -> Result<(), std::io::Error> {
writeln!(w, "//! {}#{}", target, section.id)?;
writeln!(w, "//! {}#{}{}", target, anchor_prefix(section.id.value), section.id)?;
writeln!(w, "//!")?;
writeln!(w, "//! {}", section.full_title)?;
writeln!(w, "//!")?;
Expand All @@ -311,7 +311,7 @@ fn write_rust<W: std::io::Write>(
writeln!(w)?;

for feature in features {
writeln!(w, "//= {}#{}", target, section.id)?;
writeln!(w, "//= {}#{}{}", target, anchor_prefix(section.id.value), section.id)?;
writeln!(w, "//= type=spec")?;
writeln!(w, "//= level={}", feature.level)?;
for line in feature.quote.iter() {
Expand All @@ -329,7 +329,7 @@ fn write_toml<W: std::io::Write>(
section: &Section,
features: &[Feature],
) -> Result<(), std::io::Error> {
writeln!(w, "target = \"{}#{}\"", target, section.id)?;
writeln!(w, "target = \"{}#{}{}\"", target, anchor_prefix(section.id.value), section.id)?;
writeln!(w)?;
writeln!(w, "# {}", section.full_title)?;
writeln!(w, "#")?;
Expand All @@ -351,3 +351,11 @@ fn write_toml<W: std::io::Write>(

Ok(())
}

fn anchor_prefix(id: &str) -> &str {
match id.chars().next().unwrap_or('_') {
'0'..='9' => "section-",
'A'..='Z' | 'a'..='z' => "appendix-",
_ => "",
}
}
12 changes: 11 additions & 1 deletion www/src/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,21 @@ input.annotations.forEach((anno, id) => {
anno.isOk = anno.isComplete || anno.exception === anno.spec;
}

function anchorPrefix(id) {
const c = id.charCodeAt(0);
// 1-9
if (c >= 49 && c <= 57) return "section-";
// A-Z or a-z
if (c >= 65 && c <= 90) return "appendix-";
if (c >= 97 && c <= 122) return "appendix-";
return "";
}

anno.id = id;
anno.source = blobLinker(anno);
anno.specification = specifications[anno.target_path];
anno.section = anno.specification.sections[`section-${anno.target_section}`];
anno.target = `${anno.specification.id}#${anno.section.id}`;
anno.target = `${anno.specification.id}#${anchorPrefix(anno.section.id)}${anno.section.id}`;
anno.features = [];
anno.tracking_issues = [];
anno.tags = anno.tags || [];
Expand Down

0 comments on commit 93c7875

Please sign in to comment.