⎇ d3649bfd1a80153652c2a028315369bb4b184024
crates/modules/pluma/pluma-md/src/lib.rs
Ñawpa willay
Pi imata llamkarqan
ch'uya
feat(gioser): sol detrás, título central, drawers MD + pluma agnóstico
1//! Pluma — parser markdown agnóstico, listo para envolver en cualquier viewer.
2//!
3//! Es deliberadamente delgado: wrappea `pulldown-cmark` (todas las
4//! extensiones GFM habilitadas) y emite HTML envuelto en `<div class="pluma-doc">`
5//! con un `data-pluma-theme="…"` para que el CSS del viewer aplique colores
6//! por tema sin necesidad de re-renderear.
7//!
8//! No tiene deps de web/DOM/wasm: corre igual en server, terminal, WASM o
9//! tests. Si necesitás emitir Markdown-AST en lugar de HTML, usá la API
10//! `events()` y construí tu propio renderer.
11
12use pulldown_cmark::{html, Event, Options, Parser};
13
14/// Opciones por default — GFM completo: tables, footnotes, tasklists, strikethrough,
15/// smart punctuation, heading anchors.
16pub fn default_options() -> Options {
17 Options::ENABLE_TABLES
18 | Options::ENABLE_FOOTNOTES
19 | Options::ENABLE_STRIKETHROUGH
20 | Options::ENABLE_TASKLISTS
21 | Options::ENABLE_SMART_PUNCTUATION
22 | Options::ENABLE_HEADING_ATTRIBUTES
23}
24
25/// Markdown → HTML "crudo" (sin wrapper de tema).
26pub fn to_html(md: &str) -> String {
27 let mut out = String::with_capacity(md.len() * 2);
28 let parser = Parser::new_ext(md, default_options());
29 html::push_html(&mut out, parser);
30 out
31}
32
33/// Markdown → HTML envuelto en `<div class="pluma-doc" data-pluma-theme="…">`.
34/// El `theme` es un string opaco (ej. "aire", "fuego") que el CSS del viewer
35/// matchea via `[data-pluma-theme="aire"]`.
36pub fn to_themed_html(md: &str, theme: &str) -> String {
37 let body = to_html(md);
38 let safe_theme: String = theme
39 .chars()
40 .filter(|c| c.is_ascii_alphanumeric() || *c == '-' || *c == '_')
41 .collect();
42 format!(
43 r#"<article class="pluma-doc" data-pluma-theme="{theme}">{body}</article>"#,
44 theme = safe_theme,
45 body = body
46 )
47}
48
49/// Devuelve un iterador de eventos pulldown-cmark (AST stream).
50/// Útil si querés renderear a algo distinto que HTML.
51pub fn events(md: &str) -> impl Iterator<Item = Event<'_>> {
52 Parser::new_ext(md, default_options())
53}
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn renders_h1() {
61 let html = to_html("# Hola");
62 assert!(html.contains("<h1>Hola</h1>"), "got {}", html);
63 }
64
65 #[test]
66 fn renders_list() {
67 let html = to_html("- a\n- b\n");
68 assert!(html.contains("<li>a</li>"));
69 assert!(html.contains("<li>b</li>"));
70 }
71
72 #[test]
73 fn themed_wrapper_sanitizes_theme_name() {
74 let html = to_themed_html("# x", "aire<script>");
75 assert!(html.contains(r#"data-pluma-theme="airescript""#));
76 }
77
78 #[test]
79 fn renders_code_fence() {
80 let html = to_html("```rust\nfn main(){}\n```");
81 assert!(html.contains("<pre><code") && html.contains("fn main"));
82 }
83
84 #[test]
85 fn renders_table_gfm() {
86 let md = "| a | b |\n|---|---|\n| 1 | 2 |\n";
87 let html = to_html(md);
88 assert!(html.contains("<table>"), "got {}", html);
89 }
90}