use std::collections::HashMap;
use std::path::Path;
use comrak::nodes::{AstNode, ListType, NodeList, NodeValue};
use comrak::{parse_document, Arena, Options};
use docx_rs::*;
use crate::styles::{self, BULLET_NUM_ID};
use crate::{ConvertError, ConvertOptions};
mod alerts;
mod asvg;
pub(crate) mod captions;
mod colormap;
mod endnotes;
mod frontmatter;
mod highlight;
mod imagealt;
mod images;
mod mathsplice;
mod omml;
mod proofing;
mod tableheader;
mod tables;
pub(crate) use asvg::{inject as inject_svg_layers, SvgEmbed};
pub(crate) use frontmatter::{inject as inject_core_props, DocMeta};
pub(crate) use imagealt::{inject as inject_image_alts, ImageAlt};
pub(crate) use mathsplice::{inject as inject_math, MathEmbed};
pub(crate) use proofing::inject as inject_proofing_language;
pub(crate) use tableheader::inject as inject_table_headers;
#[derive(Debug, Clone, Default)]
pub struct RenderStats {
pub headings: usize,
pub paragraphs: usize,
pub list_items: usize,
pub tables: usize,
pub code_blocks: usize,
pub images_embedded: usize,
pub images_failed: usize,
pub endnotes: usize,
pub math_spans: usize,
pub raw_html_skipped: usize,
pub warnings: Vec<String>,
}
impl RenderStats {
pub fn summary(&self) -> String {
format!(
"{} headings, {} paragraphs, {} list items, {} tables, {} code blocks, {} images ({} failed), {} endnotes, {} math spans",
self.headings,
self.paragraphs,
self.list_items,
self.tables,
self.code_blocks,
self.images_embedded,
self.images_failed,
self.endnotes,
self.math_spans,
)
}
}
#[derive(Debug, Clone, Copy, Default)]
pub(crate) struct Inline {
bold: bool,
italic: bool,
strike: bool,
superscript: bool,
subscript: bool,
underline: bool,
highlight: bool,
}
impl Inline {
fn bolded(self) -> Self {
Inline { bold: true, ..self }
}
fn italicized(self) -> Self {
Inline {
italic: true,
..self
}
}
fn struck(self) -> Self {
Inline {
strike: true,
..self
}
}
fn superscripted(self) -> Self {
Inline {
superscript: true,
..self
}
}
fn subscripted(self) -> Self {
Inline {
subscript: true,
..self
}
}
}
pub(crate) struct Ctx<'a> {
pub opts: &'a ConvertOptions,
pub base: &'a Path,
pub endnote_defs: HashMap<String, &'a AstNode<'a>>,
pub endnote_order: Vec<String>,
pub endnote_numbers: HashMap<String, usize>,
pub next_bookmark_id: usize,
pub heading_slugs: HashMap<String, usize>,
pub caption_labels: HashMap<String, String>,
pub quote_depth: usize,
pub content_width_dxa: usize,
pub svg_embeds: Vec<SvgEmbed>,
pub math_embeds: Vec<MathEmbed>,
pub image_alts: Vec<ImageAlt>,
pub list_numberings: Vec<Numbering>,
pub next_num_id: usize,
pub stats: RenderStats,
}
impl<'a> Ctx<'a> {
pub(crate) fn bookmark_id(&mut self) -> usize {
let id = self.next_bookmark_id;
self.next_bookmark_id += 1;
id
}
pub(crate) fn heading_slug(&mut self, text: &str) -> String {
let base = slugify(text);
let count = self.heading_slugs.entry(base.clone()).or_insert(0);
let slug = if *count == 0 {
base.clone()
} else {
format!("{base}-{count}")
};
*count += 1;
slug
}
}
pub(crate) fn slugify(text: &str) -> String {
let mut s = String::with_capacity(text.len());
for c in text.chars() {
if c.is_alphanumeric() {
s.extend(c.to_lowercase());
} else if c == ' ' || c == '-' {
s.push(' ');
}
}
s.split_whitespace().collect::<Vec<_>>().join("-")
}
fn quote_style(mut p: Paragraph, depth: usize) -> Paragraph {
let left = styles::QUOTE_INDENT_DXA * depth as i32;
p = p.indent(Some(left), None, None, None);
p = quote_border(p);
p
}
fn quote_border(mut p: Paragraph) -> Paragraph {
let border = ParagraphBorder::new(ParagraphBorderPosition::Left)
.size(styles::QUOTE_BORDER_SIZE)
.space(styles::QUOTE_BORDER_SPACE)
.color(styles::QUOTE_BORDER_COLOR);
let borders = ParagraphBorders::with_empty().set(border);
p.property = p.property.set_borders(borders);
p
}
fn tint_quote(run: Run, ctx: &Ctx) -> Run {
if ctx.quote_depth > 0 {
run.color(styles::QUOTE_TEXT_COLOR)
} else {
run
}
}
fn quote_indent(depth: usize) -> i32 {
styles::QUOTE_INDENT_DXA * depth as i32
}
enum Block {
Body(Paragraph),
Para(Paragraph),
Table(Table),
Gap,
}
fn push_gap(out: &mut Vec<Block>) {
match out.last() {
None | Some(Block::Gap) => return,
Some(Block::Body(_)) => {
if let Some(Block::Body(p)) = out.pop() {
out.push(Block::Body(p.line_spacing(styles::tight_after())));
}
}
_ => {}
}
out.push(Block::Gap);
}
pub(crate) enum InlineChild {
Run(Run),
Hyperlink(Hyperlink),
}
impl InlineChild {
pub(crate) fn run(r: Run) -> Self {
InlineChild::Run(r)
}
}
pub(crate) fn add_inline(p: Paragraph, child: InlineChild) -> Paragraph {
match child {
InlineChild::Run(r) => p.add_run(r),
InlineChild::Hyperlink(h) => p.add_hyperlink(h),
}
}
pub(crate) fn bold_inline(child: InlineChild) -> InlineChild {
match child {
InlineChild::Run(r) => InlineChild::Run(r.bold()),
InlineChild::Hyperlink(mut h) => {
h.children = h
.children
.into_iter()
.map(|c| match c {
ParagraphChild::Run(r) => ParagraphChild::Run(Box::new((*r).bold())),
other => other,
})
.collect();
InlineChild::Hyperlink(h)
}
}
}
pub(crate) type BuildOutput = (
Docx,
RenderStats,
Vec<SvgEmbed>,
Vec<ImageAlt>,
DocMeta,
Vec<MathEmbed>,
);
pub(crate) fn build_docx(
markdown: &str,
opts: &ConvertOptions,
base: &Path,
) -> Result<BuildOutput, ConvertError> {
let arena = Arena::new();
let options = comrak_options();
let root = parse_document(&arena, markdown, &options);
let doc_meta = frontmatter::parse(root);
let mut ctx = Ctx {
opts,
base,
endnote_defs: HashMap::new(),
endnote_order: Vec::new(),
endnote_numbers: HashMap::new(),
next_bookmark_id: 1,
heading_slugs: HashMap::new(),
caption_labels: HashMap::new(),
quote_depth: 0,
content_width_dxa: opts.page.content_width_dxa(),
svg_embeds: Vec::new(),
math_embeds: Vec::new(),
image_alts: Vec::new(),
list_numberings: Vec::new(),
next_num_id: styles::FIRST_LIST_NUM_ID,
stats: RenderStats::default(),
};
endnotes::collect(root, &mut ctx);
captions::collect(root, &mut ctx);
let mut blocks = Vec::new();
render_blocks(root, &mut ctx, &mut blocks);
endnotes::render_section(&mut ctx, &mut blocks);
if matches!(blocks.first(), Some(Block::Gap)) {
blocks.remove(0);
}
if matches!(blocks.last(), Some(Block::Gap)) {
blocks.pop();
}
let mut docx = styles::apply(Docx::new(), &opts.page, &opts.theme);
if opts.page_numbers {
docx = docx.footer(page_number_footer());
}
for numbering in std::mem::take(&mut ctx.list_numberings) {
docx = docx.add_numbering(numbering);
}
if opts.table_of_contents {
docx = docx.add_paragraph(table_of_contents_title());
docx = docx.add_table_of_contents(
TableOfContents::new()
.heading_styles_range(1, 3)
.hyperlink()
.dirty(),
);
docx = docx.add_paragraph(Paragraph::new().add_run(Run::new().add_break(BreakType::Page)));
}
for b in blocks {
docx = match b {
Block::Body(p) => docx.add_paragraph(p),
Block::Para(p) => docx.add_paragraph(p),
Block::Table(t) => docx.add_table(t),
Block::Gap => docx.add_paragraph(styles::block_gap_paragraph()),
};
}
Ok((
docx,
ctx.stats,
ctx.svg_embeds,
ctx.image_alts,
doc_meta,
ctx.math_embeds,
))
}
fn page_number_footer() -> Footer {
let para = Paragraph::new()
.align(AlignmentType::Center)
.line_spacing(styles::tight_after())
.add_run(Run::new().add_text("Page "))
.add_page_num(PageNum::new())
.add_run(Run::new().add_text(" of "))
.add_num_pages(NumPages::new());
Footer::new().add_paragraph(para)
}
fn table_of_contents_title() -> Paragraph {
Paragraph::new()
.line_spacing(styles::body_spacing())
.add_run(Run::new().bold().size(28).add_text("Contents"))
}
fn comrak_options() -> Options<'static> {
comrak_options_pub()
}
pub(crate) fn comrak_options_pub() -> Options<'static> {
let mut o = Options::default();
o.extension.table = true;
o.extension.strikethrough = true;
o.extension.tasklist = true;
o.extension.autolink = true;
o.extension.footnotes = true;
o.extension.superscript = true;
o.extension.subscript = true;
o.extension.alerts = true;
o.extension.math_dollars = true;
o.extension.math_code = true;
o.extension.front_matter_delimiter = Some("---".to_string());
o
}
fn render_blocks<'a>(container: &'a AstNode<'a>, ctx: &mut Ctx, out: &mut Vec<Block>) {
for child in container.children() {
let value = child.data.borrow().value.clone();
match value {
NodeValue::Heading(h) => {
let mut runs = Vec::new();
render_inlines(child, Inline::default(), &mut runs, ctx);
let slug = ctx.heading_slug(&text_of(child));
let bid = ctx.bookmark_id();
let mut p = Paragraph::new()
.style(heading_style_id(h.level))
.keep_next(true)
.keep_lines(true)
.add_bookmark_start(bid, slug);
for r in runs {
p = add_inline(p, r);
}
p = p.add_bookmark_end(bid);
if ctx.quote_depth > 0 {
p = quote_style(p, ctx.quote_depth);
}
out.push(Block::Para(p));
ctx.stats.headings += 1;
}
NodeValue::Paragraph => {
if ctx.opts.captions && ctx.quote_depth == 0 {
if let Some(cap) = captions::caption_of(&text_of(child)) {
out.push(Block::Para(captions::paragraph(&cap, ctx)));
ctx.stats.paragraphs += 1;
continue;
}
}
let mut runs = Vec::new();
render_inlines(child, Inline::default(), &mut runs, ctx);
let mut p = Paragraph::new();
if ctx.quote_depth == 0 && is_display_math_paragraph(child) {
p = p.align(AlignmentType::Center);
}
for r in runs {
p = add_inline(p, r);
}
if ctx.quote_depth > 0 {
p = quote_style(p, ctx.quote_depth);
out.push(Block::Para(p));
} else {
out.push(Block::Body(p));
}
ctx.stats.paragraphs += 1;
}
NodeValue::List(list) => {
render_list(child, &list, 0, ctx, out);
}
NodeValue::CodeBlock(cb) => {
let is_math_fence = cb.info.split_whitespace().next() == Some("math");
if is_math_fence {
ctx.stats.math_spans += 1;
match omml::latex_to_omml(&cb.literal, true) {
Ok(xml) => {
let sentinel = mathsplice::sentinel(ctx.math_embeds.len());
ctx.math_embeds.push(MathEmbed {
sentinel: sentinel.clone(),
omml: xml,
});
push_gap(out);
out.push(Block::Para(
Paragraph::new()
.align(AlignmentType::Center)
.add_run(Run::new().add_text(sentinel)),
));
push_gap(out);
continue;
}
Err(e) => warn_math_fallback(ctx, &e),
}
}
push_gap(out);
let mut t = code_block(
&cb.literal,
&cb.info,
ctx.content_width_dxa,
ctx.opts.highlight_code,
&ctx.opts.theme,
);
if ctx.quote_depth > 0 {
t = t.indent(quote_indent(ctx.quote_depth));
}
out.push(Block::Table(t));
push_gap(out);
ctx.stats.code_blocks += 1;
}
NodeValue::Table(_) => {
push_gap(out);
let mut t = tables::build(child, ctx);
if ctx.quote_depth > 0 {
t = t.indent(quote_indent(ctx.quote_depth));
}
out.push(Block::Table(t));
push_gap(out);
ctx.stats.tables += 1;
}
NodeValue::ThematicBreak => {
push_gap(out);
out.push(Block::Table(horizontal_rule(ctx.content_width_dxa)));
push_gap(out);
}
NodeValue::Alert(alert) => {
push_gap(out);
out.push(Block::Table(alerts::build(child, &alert, ctx)));
push_gap(out);
}
NodeValue::BlockQuote => {
let top_level = ctx.quote_depth == 0;
if top_level {
push_gap(out);
}
ctx.quote_depth += 1;
render_blocks(child, ctx, out);
ctx.quote_depth -= 1;
if top_level {
push_gap(out);
}
}
NodeValue::FootnoteDefinition(_) => {}
NodeValue::FrontMatter(_) => {}
NodeValue::HtmlBlock(_) => ctx.stats.raw_html_skipped += 1,
_ => render_blocks(child, ctx, out),
}
}
}
fn render_list<'a>(
list_node: &'a AstNode<'a>,
list: &NodeList,
depth: usize,
ctx: &mut Ctx,
out: &mut Vec<Block>,
) {
let level = depth.min(8);
let num_id = match list.list_type {
ListType::Ordered => {
let id = ctx.next_num_id;
ctx.next_num_id += 1;
ctx.list_numberings
.push(styles::ordered_numbering(id, level, list.start));
id
}
ListType::Bullet => BULLET_NUM_ID,
};
let item_line = if list.tight {
styles::tight_after()
} else {
styles::body_spacing()
};
let continuation_indent = styles::LIST_INDENT_STEP_DXA * (level as i32 + 1);
for item in list_node.children() {
let task_symbol = match item.data.borrow().value {
NodeValue::TaskItem(ref t) => Some(t.symbol),
_ => None,
};
let mut marker_used = false;
for block in item.children() {
let value = block.data.borrow().value.clone();
match value {
NodeValue::Paragraph => {
let mut runs = Vec::new();
render_inlines(block, Inline::default(), &mut runs, ctx);
let mut p = if marker_used {
Paragraph::new().line_spacing(item_line.clone()).indent(
Some(continuation_indent),
None,
None,
None,
)
} else if let Some(sym) = task_symbol {
let glyph = if sym.is_some() {
styles::TASK_CHECKED
} else {
styles::TASK_UNCHECKED
};
let left = styles::LIST_INDENT_STEP_DXA * (level as i32 + 1);
let mut tp = Paragraph::new().line_spacing(item_line.clone()).indent(
Some(left),
Some(SpecialIndentType::Hanging(styles::LIST_HANGING_DXA)),
None,
None,
);
tp = add_inline(tp, InlineChild::run(Run::new().add_text(glyph).add_tab()));
tp
} else {
Paragraph::new()
.line_spacing(item_line.clone())
.numbering(NumberingId::new(num_id), IndentLevel::new(level))
};
marker_used = true;
for r in runs {
p = add_inline(p, r);
}
if ctx.quote_depth > 0 {
p = quote_border(p);
}
out.push(Block::Para(p));
}
NodeValue::List(sub) => {
render_list(block, &sub, depth + 1, ctx, out);
}
_ => render_blocks(block, ctx, out),
}
}
ctx.stats.list_items += 1;
}
}
pub(crate) fn render_inlines<'a>(
container: &'a AstNode<'a>,
style: Inline,
out: &mut Vec<InlineChild>,
ctx: &mut Ctx,
) {
let mut html = HtmlInlineState::default();
for child in container.children() {
let value = child.data.borrow().value.clone();
let cur = html.apply(style);
match value {
NodeValue::Text(t) => {
out.push(InlineChild::run(tint_quote(styled(cur).add_text(t), ctx)))
}
NodeValue::Emph => render_inlines(child, cur.italicized(), out, ctx),
NodeValue::Strong => render_inlines(child, cur.bolded(), out, ctx),
NodeValue::Strikethrough => render_inlines(child, cur.struck(), out, ctx),
NodeValue::Superscript => render_inlines(child, cur.superscripted(), out, ctx),
NodeValue::Subscript => render_inlines(child, cur.subscripted(), out, ctx),
NodeValue::Code(code) => out.push(InlineChild::run(mono_run(
&code.literal,
ctx.opts.theme.mono_font,
))),
NodeValue::Math(math) => {
ctx.stats.math_spans += 1;
out.push(InlineChild::run(math_run(
&math.literal,
math.display_math,
ctx,
)));
}
NodeValue::SoftBreak => {
out.push(InlineChild::run(tint_quote(styled(cur).add_text(" "), ctx)))
}
NodeValue::LineBreak => out.push(InlineChild::run(
Run::new().add_break(BreakType::TextWrapping),
)),
NodeValue::HtmlInline(raw) => html.consume(&raw, out, ctx),
NodeValue::Link(link) => {
if ctx.opts.captions {
if let Some(label) = link.url.strip_prefix('#') {
if let Some(r) = captions::reference(label, &text_of(child), ctx) {
out.push(InlineChild::run(r));
continue;
}
}
}
let mut inner = Vec::new();
render_inlines(child, cur, &mut inner, ctx);
out.push(InlineChild::Hyperlink(build_hyperlink(
&link.url,
inner,
ctx.opts.theme.link_color,
)));
}
NodeValue::Image(link) => {
let alt = text_of(child);
out.push(InlineChild::run(images::run(
&link.url,
&alt,
&link.title,
ctx,
)));
}
NodeValue::FootnoteReference(fref) => {
out.push(endnotes::reference(&fref.name, ctx));
}
_ => render_inlines(child, cur, out, ctx),
}
}
}
#[derive(Default)]
struct HtmlInlineState {
bold: u32,
italic: u32,
underline: u32,
strike: u32,
sub: u32,
sup: u32,
mark: u32,
}
impl HtmlInlineState {
fn apply(&self, base: Inline) -> Inline {
Inline {
bold: base.bold || self.bold > 0,
italic: base.italic || self.italic > 0,
strike: base.strike || self.strike > 0,
superscript: base.superscript || self.sup > 0,
subscript: base.subscript || self.sub > 0,
underline: base.underline || self.underline > 0,
highlight: base.highlight || self.mark > 0,
}
}
fn consume(&mut self, raw: &str, out: &mut Vec<InlineChild>, ctx: &mut Ctx) {
let Some((closing, name)) = parse_html_tag(raw) else {
ctx.stats.raw_html_skipped += 1;
return;
};
let bump = |n: &mut u32| {
if closing {
*n = n.saturating_sub(1);
} else {
*n += 1;
}
};
match name.as_str() {
"br" if !closing => out.push(InlineChild::run(
Run::new().add_break(BreakType::TextWrapping),
)),
"b" | "strong" => bump(&mut self.bold),
"i" | "em" => bump(&mut self.italic),
"u" | "ins" => bump(&mut self.underline),
"s" | "del" | "strike" => bump(&mut self.strike),
"sub" => bump(&mut self.sub),
"sup" => bump(&mut self.sup),
"mark" => bump(&mut self.mark),
_ => ctx.stats.raw_html_skipped += 1,
}
}
}
fn parse_html_tag(raw: &str) -> Option<(bool, String)> {
let t = raw.trim().strip_prefix('<')?.strip_suffix('>')?;
let (closing, rest) = match t.strip_prefix('/') {
Some(r) => (true, r),
None => (false, t),
};
let name: String = rest
.trim_start()
.chars()
.take_while(|c| c.is_ascii_alphanumeric())
.collect::<String>()
.to_ascii_lowercase();
(!name.is_empty()).then_some((closing, name))
}
fn build_hyperlink(url: &str, inner: Vec<InlineChild>, link_color: &str) -> Hyperlink {
let mut link = if let Some(anchor) = url.strip_prefix('#') {
Hyperlink::new(anchor, HyperlinkType::Anchor)
} else {
Hyperlink::new(url, HyperlinkType::External)
};
for c in inner {
match c {
InlineChild::Run(r) => {
link = link.add_run(r.color(link_color).underline("single"));
}
InlineChild::Hyperlink(nested) => {
for nc in nested.children {
if let ParagraphChild::Run(r) = nc {
link = link.add_run(*r);
}
}
}
}
}
link
}
fn styled(s: Inline) -> Run {
let mut r = Run::new();
if s.bold {
r = r.bold();
}
if s.italic {
r = r.italic();
}
if s.strike {
r = r.strike();
}
if s.underline {
r = r.underline("single");
}
if s.highlight {
r = r.highlight("yellow");
}
if s.superscript {
r.run_property = r.run_property.vert_align(VertAlignType::SuperScript);
} else if s.subscript {
r.run_property = r.run_property.vert_align(VertAlignType::SubScript);
}
r
}
fn mono_run(text: &str, mono_font: &str) -> Run {
Run::new()
.fonts(RunFonts::new().ascii(mono_font).hi_ansi(mono_font))
.size(styles::CODE_SIZE)
.add_text(text)
}
fn is_display_math_paragraph<'a>(para: &'a AstNode<'a>) -> bool {
let mut saw_display_math = false;
for child in para.children() {
match &child.data.borrow().value {
NodeValue::Math(m) if m.display_math => saw_display_math = true,
NodeValue::Text(t) if t.trim().is_empty() => {}
NodeValue::SoftBreak | NodeValue::LineBreak => {}
_ => return false,
}
}
saw_display_math
}
fn math_run(literal: &str, display: bool, ctx: &mut Ctx) -> Run {
match omml::latex_to_omml(literal, display) {
Ok(xml) => {
let sentinel = mathsplice::sentinel(ctx.math_embeds.len());
ctx.math_embeds.push(MathEmbed {
sentinel: sentinel.clone(),
omml: xml,
});
Run::new().add_text(sentinel)
}
Err(e) => {
warn_math_fallback(ctx, &e);
math_literal_run(literal, ctx.opts.theme.mono_font)
}
}
}
fn warn_math_fallback(ctx: &mut Ctx, reason: &str) {
if !ctx.stats.warnings.iter().any(|w| w.starts_with("math:")) {
ctx.stats
.warnings
.push(format!("math: rendered as literal LaTeX source ({reason})"));
}
}
fn math_literal_run(text: &str, mono_font: &str) -> Run {
mono_run(text, mono_font).italic()
}
fn code_block(
literal: &str,
info: &str,
content_width_dxa: usize,
highlight: bool,
theme: &crate::Theme,
) -> Table {
let mut cell = TableCell::new().shading(Shading::new().fill(theme.code_fill));
let trimmed = literal.strip_suffix('\n').unwrap_or(literal);
let highlighted = if highlight {
highlight::language_token(info)
.and_then(|lang| highlight::highlight(trimmed, lang, theme.highlight_theme))
} else {
None
};
if highlight {
if let Some(label) = highlight::display_label(info) {
cell = cell.add_paragraph(code_label(&label, theme.mono_font));
}
}
match highlighted {
Some(lines) => {
for spans in lines {
let mut p = Paragraph::new().line_spacing(styles::code_spacing());
if spans.is_empty() {
p = p.add_run(mono_run("", theme.mono_font));
} else {
for (color, text) in spans {
p = p.add_run(mono_run(&text, theme.mono_font).color(color));
}
}
cell = cell.add_paragraph(p);
}
}
None => {
for line in trimmed.split('\n') {
cell = cell.add_paragraph(
Paragraph::new()
.line_spacing(styles::code_spacing())
.add_run(mono_run(line, theme.mono_font)),
);
}
}
}
Table::new(vec![TableRow::new(vec![cell])])
.width(content_width_dxa, WidthType::Dxa)
.margins(styles::code_cell_margins())
}
fn code_label(label: &str, mono_font: &str) -> Paragraph {
Paragraph::new()
.line_spacing(styles::code_spacing())
.add_run(
Run::new()
.fonts(RunFonts::new().ascii(mono_font).hi_ansi(mono_font))
.size(styles::CODE_LABEL_SIZE)
.bold()
.color(styles::QUOTE_TEXT_COLOR)
.add_text(label),
)
}
fn horizontal_rule(content_width_dxa: usize) -> Table {
use TableBorderPosition::*;
let bottom = TableBorder::new(Bottom)
.border_type(BorderType::Single)
.size(4)
.color(styles::TABLE_BORDER_COLOR);
let borders = TableBorders::with_empty().set(bottom);
let cell = TableCell::new().add_paragraph(Paragraph::new());
Table::new(vec![TableRow::new(vec![cell])])
.width(content_width_dxa, WidthType::Dxa)
.set_borders(borders)
}
fn heading_style_id(level: u8) -> &'static str {
match level {
1 => "Heading1",
2 => "Heading2",
3 => "Heading3",
4 => "Heading4",
5 => "Heading5",
_ => "Heading6",
}
}
pub(crate) fn text_of<'a>(node: &'a AstNode<'a>) -> String {
let mut s = String::new();
for d in node.descendants() {
if let NodeValue::Text(t) = &d.data.borrow().value {
s.push_str(t);
}
}
s
}