Files
nuwiki/crates/nuwiki-core/tests/vimwiki_table_alignment.rs
T
gffranco 214d54e3b9 parity(7): table cell alignment markers (|:--|--:|:--:|)
The Markdown-style alignment anchors on a table's header-separator
row now propagate from the lexer through the parser into the AST,
and the HTML renderer emits a `style="text-align: …;"` attribute on
each affected `<th>` / `<td>`.

  |:--|   → left
  |--:|   → right
  |:--:|  → center
  |---|   → default (no style attr)

Encoding choice: rather than threading the source string into the
parser, the lexer now extracts per-cell alignment when it recognises
the separator row and carries the `Vec<TableAlign>` inline on the
`TableHeaderRow` token. The parser unpacks it into `TableNode`'s
new `alignments: Vec<TableAlign>` field. Cells past the end of the
vector render with the default alignment.

`TableAlign` lives in `nuwiki_core::ast::block` alongside the table
nodes and is re-exported via `nuwiki_core::ast`.

Tests: 4 new in crates/nuwiki-core/tests/vimwiki_table_alignment.rs
covering plain dashes, all three anchor flavours, the HTML output
shape, and the no-style-attr case. The existing
`vimwiki_lexer::table_header_separator_row` was updated to assert
the new payload shape.

Gates: 425 Rust / 39 Neovim / 12 Vim.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-12 21:45:43 +00:00

58 lines
1.8 KiB
Rust

//! Cluster 7 — Markdown-style alignment markers on the header
//! separator row (`|:--|--:|:--:|`) propagate to the AST and HTML.
use nuwiki_core::ast::{BlockNode, TableAlign};
use nuwiki_core::render::{HtmlRenderer, Renderer};
use nuwiki_core::syntax::vimwiki::VimwikiSyntax;
use nuwiki_core::syntax::SyntaxPlugin;
fn parse(src: &str) -> nuwiki_core::ast::DocumentNode {
VimwikiSyntax::new().parse(src)
}
#[test]
fn separator_row_with_no_anchors_yields_defaults() {
let doc = parse("| h1 | h2 |\n|----|----|\n| a | b |\n");
let table = match &doc.children[0] {
BlockNode::Table(t) => t,
_ => panic!("expected table"),
};
assert_eq!(
table.alignments,
vec![TableAlign::Default, TableAlign::Default]
);
}
#[test]
fn left_right_center_anchors_parsed() {
let doc = parse("| a | b | c |\n|:---|---:|:---:|\n| 1 | 2 | 3 |\n");
let table = match &doc.children[0] {
BlockNode::Table(t) => t,
_ => panic!("expected table"),
};
assert_eq!(
table.alignments,
vec![TableAlign::Left, TableAlign::Right, TableAlign::Center]
);
}
#[test]
fn html_renderer_emits_text_align_style() {
let doc = parse("| a | b | c |\n|:---|---:|:---:|\n| 1 | 2 | 3 |\n");
let out = HtmlRenderer::new().render_to_string(&doc).unwrap();
assert!(
out.contains(r#"<th style="text-align: left;"> a </th>"#),
"got: {out}"
);
assert!(out.contains(r#"<th style="text-align: right;"> b </th>"#));
assert!(out.contains(r#"<th style="text-align: center;"> c </th>"#));
assert!(out.contains(r#"<td style="text-align: left;"> 1 </td>"#));
}
#[test]
fn render_without_alignment_omits_style_attr() {
let doc = parse("| a | b |\n|---|---|\n| 1 | 2 |\n");
let out = HtmlRenderer::new().render_to_string(&doc).unwrap();
assert!(!out.contains("text-align"));
}