Skip to content

Commit

Permalink
fix(converter): remove possible links from the produced HTML heading IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
NTBBloodbath committed Feb 8, 2025
1 parent 1264717 commit b8e81c8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ minify-html = "0.15.0"
num_cpus = "1.16.0"
minify-js = "0.6.0"
css-minify = "0.5.2"
regex = "1.11.1"

[dev-dependencies]
mockall = "0.13.1"
Expand Down
14 changes: 11 additions & 3 deletions src/converter/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// so we are going to omit them for now until it's fixed.

use html_escape::encode_text_minimal_to_string;
use regex::Regex;
use rust_norg::{
parse_tree, CarryoverTag, DelimitingModifier, LinkTarget, NestableDetachedModifier, NorgAST, NorgASTFlat,
ParagraphSegment, ParagraphSegmentToken,
Expand Down Expand Up @@ -285,15 +286,22 @@ impl NorgToHtml for NorgAST {
..
} => {
let mut section = Vec::<String>::new();
let heading_title = paragraph_to_string(title, &strong_carry, &mut weak_carry);
// HACK: we are passing empty carryover vectors here because otherwise
// the HTML carryovers meant for the heading are used for its internal content instead
let strong = Vec::<CarryOverTag>::new();
let mut weak = Vec::<CarryOverTag>::new();
let heading_title = paragraph_to_string(title, &strong, &mut weak);

// Regex to remove possible links from heading title ids
let re = Regex::new(r"-<.*>").unwrap();

match level {
1..=6 => {
section.push(format!(
"<h{} id=\"{}\"",
level,
heading_title.replace(" ", "-")
));
re.replace(&heading_title.replace(" ", "-"), "")
));
if !weak_carry.is_empty() {
for weak_carryover in weak_carry.clone() {
section.push(weak_carryover_attribute(weak_carryover));
Expand Down

0 comments on commit b8e81c8

Please sign in to comment.