tabnas_markdown/options.rs
1/* Copyright (c) 2021-2026 Richard Rodger, MIT License */
2
3//! Types shared by both parse phases. Port of `ts/src/options.ts` and
4//! `go/options.go`.
5
6use std::collections::HashMap;
7
8/// The parse options. [`Default`] is this package's defaults (`gfm` on,
9/// `breaks` off), the same pair as `Markdown.defaults` in TypeScript and
10/// `Defaults` in Go.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub struct Options {
13 /// Enable the GFM extensions. Default true.
14 ///
15 /// Five are implemented, and this one flag gates all of them together:
16 /// tables, strikethrough (`~~x~~`), task list items (`- [x] foo`),
17 /// autolink literals (bare `www.` / `http://` / `https://` / `ftp://` /
18 /// `a@b.co`) and the disallowed-raw-HTML filter. Footnotes are not
19 /// implemented.
20 ///
21 /// The first four are parse-time; the raw-HTML filter is applied by the
22 /// renderer, which is why `html.rs` reads this option too. With `gfm`
23 /// off the output is plain CommonMark, byte for byte.
24 pub gfm: bool,
25 /// Render soft line breaks as hard breaks. Default false.
26 pub breaks: bool,
27}
28
29impl Default for Options {
30 fn default() -> Self {
31 DEFAULT_OPTIONS
32 }
33}
34
35/// The package defaults: `gfm` on, `breaks` off.
36pub const DEFAULT_OPTIONS: Options = Options {
37 gfm: true,
38 breaks: false,
39};
40
41impl Options {
42 /// Plain CommonMark: every extension off. What the conformance corpus
43 /// runs with.
44 pub const COMMONMARK: Options = Options {
45 gfm: false,
46 breaks: false,
47 };
48
49 /// Apply the defaults to a plugin option bag. Unknown keys are
50 /// ignored; a key present with a non-boolean value keeps the default.
51 /// This is the Rust spelling of `resolveOptions` / `ResolveOptions`.
52 pub fn resolve(bag: &tabnas::Value) -> Options {
53 let mut out = DEFAULT_OPTIONS;
54 let tabnas::Value::Object(map) = bag else {
55 return out;
56 };
57 if let Some(tabnas::Value::Bool(gfm)) = map.get("gfm") {
58 out.gfm = *gfm;
59 }
60 if let Some(tabnas::Value::Bool(breaks)) = map.get("breaks") {
61 out.breaks = *breaks;
62 }
63 out
64 }
65
66 /// The same resolution over a JSON bag, which is how the shared
67 /// fixtures and the golden files spell a row's options.
68 pub fn resolve_json(bag: &serde_json::Value) -> Options {
69 let mut out = DEFAULT_OPTIONS;
70 if let Some(gfm) = bag.get("gfm").and_then(serde_json::Value::as_bool) {
71 out.gfm = gfm;
72 }
73 if let Some(breaks) = bag.get("breaks").and_then(serde_json::Value::as_bool) {
74 out.breaks = breaks;
75 }
76 out
77 }
78
79 /// The options as a plugin option bag, for `Tabnas::use_plugin`.
80 pub fn to_value(self) -> tabnas::Value {
81 let mut map = indexmap::IndexMap::new();
82 map.insert("gfm".to_string(), tabnas::Value::Bool(self.gfm));
83 map.insert("breaks".to_string(), tabnas::Value::Bool(self.breaks));
84 tabnas::Value::object(map)
85 }
86}
87
88/// A resolved link reference definition (section 4.7).
89#[derive(Debug, Clone, PartialEq, Eq)]
90pub struct RefDef {
91 pub destination: String,
92 /// `None` when the definition carried no title.
93 pub title: Option<String>,
94}
95
96/// Keys are labels put through `normalize_reference`.
97pub type RefMap = HashMap<String, RefDef>;