[Web] Add simple anchors and ids to h1, h2, h3
This commit is contained in:
parent
fc4e3a93f1
commit
4d49eea7ea
@ -1,9 +1,8 @@
|
||||
use std::fmt::format;
|
||||
|
||||
use markdown::mdast::Heading;
|
||||
|
||||
use super::Printable;
|
||||
use crate::utils;
|
||||
|
||||
use super::Printable;
|
||||
|
||||
impl Printable for Heading {
|
||||
fn to_html(&self) -> String {
|
||||
@ -15,7 +14,25 @@ impl Printable for Heading {
|
||||
|
||||
let text: String = result.into_iter().collect();
|
||||
|
||||
if self.depth < 4 {
|
||||
let html_fragment_text = utils::to_html_fragment(&self.get_text());
|
||||
|
||||
format!(
|
||||
"<h{} id=\"{}\"><a href=\"#{}\">{}</a></h{}>",
|
||||
self.depth, html_fragment_text, html_fragment_text, text, self.depth
|
||||
)
|
||||
} else {
|
||||
format!("<h{}>{}</h{}>", self.depth, text, self.depth)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_text(&self) -> String {
|
||||
let mut result = Vec::<String>::new();
|
||||
|
||||
for node in &self.children {
|
||||
result.push(node.get_text())
|
||||
}
|
||||
|
||||
result.join("-")
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,12 @@
|
||||
use markdown::mdast::Node;
|
||||
|
||||
mod root;
|
||||
mod heading;
|
||||
mod root;
|
||||
mod text;
|
||||
|
||||
pub trait Printable {
|
||||
fn to_html(&self) -> String;
|
||||
fn get_text(&self) -> String;
|
||||
}
|
||||
|
||||
impl Printable for Node {
|
||||
@ -12,8 +14,17 @@ impl Printable for Node {
|
||||
match self {
|
||||
Node::Root(root) => root.to_html(),
|
||||
Node::Heading(heading) => heading.to_html(),
|
||||
Node::Text(text) => text.to_html(),
|
||||
_ => format!("Not implemented<br>{:?}", self),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_text(&self) -> String {
|
||||
match self {
|
||||
Node::Root(root) => root.get_text(),
|
||||
Node::Heading(heading) => heading.get_text(),
|
||||
Node::Text(text) => text.get_text(),
|
||||
_ => String::from(""),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ use markdown::mdast;
|
||||
|
||||
use super::Printable;
|
||||
|
||||
|
||||
impl Printable for mdast::Root {
|
||||
fn to_html(&self) -> String {
|
||||
let mut result = Vec::<String>::new();
|
||||
@ -13,4 +12,14 @@ impl Printable for mdast::Root {
|
||||
|
||||
result.into_iter().collect()
|
||||
}
|
||||
|
||||
fn get_text(&self) -> String {
|
||||
let mut result = Vec::<String>::new();
|
||||
|
||||
for node in &self.children {
|
||||
result.push(node.get_text())
|
||||
}
|
||||
|
||||
result.join("-")
|
||||
}
|
||||
}
|
||||
|
13
doc-generator/src/generator/text.rs
Normal file
13
doc-generator/src/generator/text.rs
Normal file
@ -0,0 +1,13 @@
|
||||
use markdown::mdast::Text;
|
||||
|
||||
use super::Printable;
|
||||
|
||||
impl Printable for Text {
|
||||
fn to_html(&self) -> String {
|
||||
self.value.clone()
|
||||
}
|
||||
|
||||
fn get_text(&self) -> String {
|
||||
self.value.clone()
|
||||
}
|
||||
}
|
@ -1,14 +1,11 @@
|
||||
use clap::Parser;
|
||||
use generator::Printable;
|
||||
use markdown::to_html;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::{
|
||||
fs,
|
||||
path::Path,
|
||||
};
|
||||
use std::{fs, path::Path};
|
||||
|
||||
mod generator;
|
||||
mod utils;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(author, version, about, long_about = None)]
|
||||
@ -55,7 +52,11 @@ fn process_folder(path: &Path, input_folder: &Path, output_folder: &Path) {
|
||||
}
|
||||
}
|
||||
|
||||
fn ensure_folder_exists(folder: &Path, input_folder: &Path, output_folder: &Path) -> Result<(), String> {
|
||||
fn ensure_folder_exists(
|
||||
folder: &Path,
|
||||
input_folder: &Path,
|
||||
output_folder: &Path,
|
||||
) -> Result<(), String> {
|
||||
// /home/fernando/misti/docs/markdown
|
||||
let input_folder = input_folder.canonicalize().unwrap();
|
||||
|
||||
@ -115,7 +116,6 @@ fn process_markdown(file: &Path, input_folder: &Path, output_folder: &Path) -> R
|
||||
let md_ast = markdown::to_mdast(&markdown_text, &markdown::ParseOptions::gfm()).unwrap();
|
||||
let html_text = md_ast.to_html();
|
||||
|
||||
|
||||
// Read template.html
|
||||
let mut template_path = output_folder.clone();
|
||||
template_path.push("template.html");
|
||||
|
3
doc-generator/src/utils.rs
Normal file
3
doc-generator/src/utils.rs
Normal file
@ -0,0 +1,3 @@
|
||||
pub fn to_html_fragment(text: &String) -> String {
|
||||
text.clone().to_lowercase().replace(" ", "-")
|
||||
}
|
Loading…
Reference in New Issue
Block a user