Skip to main content

dprint_plugin_markdown/configuration/
types.rs

1use std::collections::HashMap;
2
3use dprint_core::configuration::*;
4use dprint_core::generate_str_to_from;
5use serde::Deserialize;
6use serde::Serialize;
7
8/// Resolved markdown configuration.
9#[derive(Clone, Serialize, Deserialize)]
10#[serde(rename_all = "camelCase")]
11pub struct Configuration {
12  pub line_width: u32,
13  pub new_line_kind: NewLineKind,
14  pub text_wrap: TextWrap,
15  pub emphasis_kind: EmphasisKind,
16  pub strong_kind: StrongKind,
17  pub unordered_list_kind: UnorderedListKind,
18  pub heading_kind: HeadingKind,
19  pub list_indent_kind: ListIndentKind,
20  pub ignore_directive: String,
21  pub ignore_file_directive: String,
22  pub ignore_start_directive: String,
23  pub ignore_end_directive: String,
24  /// Custom tag to file extension mappings for formatting code blocks.
25  /// For example: { "custom-tag": "md" }
26  #[serde(default)]
27  pub tags: HashMap<String, String>,
28}
29
30/// Text wrapping possibilities.
31#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
32#[serde(rename_all = "camelCase")]
33pub enum TextWrap {
34  /// Always wraps text.
35  Always,
36  /// Maintains line breaks (default).
37  Maintain,
38  /// Never wraps text.
39  Never,
40}
41
42generate_str_to_from![TextWrap, [Always, "always"], [Maintain, "maintain"], [Never, "never"]];
43
44/// The character to use for emphasis/italics.
45#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
46#[serde(rename_all = "camelCase")]
47pub enum EmphasisKind {
48  /// Uses asterisks (*) for emphasis.
49  Asterisks,
50  /// Uses underscores (_) for emphasis (default).
51  Underscores,
52}
53
54generate_str_to_from![EmphasisKind, [Asterisks, "asterisks"], [Underscores, "underscores"]];
55
56/// The character to use for strong emphasis/bold.
57#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
58#[serde(rename_all = "camelCase")]
59pub enum StrongKind {
60  /// Uses asterisks (**) for strong emphasis (default).
61  Asterisks,
62  /// Uses underscores (__) for strong emphasis.
63  Underscores,
64}
65
66generate_str_to_from![StrongKind, [Asterisks, "asterisks"], [Underscores, "underscores"]];
67
68/// The character to use primarily for lists.
69///
70/// Unnumbered lists will be formatted to use a common list character, i.e., the primary list
71/// character. Additionally, an alternate list character is used to separate lists which are not
72/// separated by other paragraphs. This parameter defines which character should be used as primary
73/// list character, i.e., either '-' (default) or '*'. The alternate list character will be the one
74/// which is _not_ primary.
75#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
76#[serde(rename_all = "camelCase")]
77pub enum UnorderedListKind {
78  /// Uses dashes (-) as primary character for lists (default).
79  ///
80  /// In this case, asterisks are used as alternate list characters.
81  Dashes,
82  /// Uses asterisks (*) as primary character for lists.
83  ///
84  /// In this case, dashes are used as alternate list characters.
85  Asterisks,
86}
87
88impl UnorderedListKind {
89  /// Determine the character to use for a list, i.e., '-' or '*'.
90  ///
91  /// The result depends on the configuration and whether the primary or alternate character is
92  /// requested. See [`Self`].
93  pub fn list_char(&self, is_alternate: bool) -> char {
94    match (self, is_alternate) {
95      (Self::Dashes, true) | (Self::Asterisks, false) => '*',
96      _ => '-',
97    }
98  }
99}
100
101generate_str_to_from![UnorderedListKind, [Dashes, "dashes"], [Asterisks, "asterisks"]];
102
103/// The style of heading to use for level 1 and level 2 headings:
104/// [setext](https://spec.commonmark.org/0.31.2/#setext-headings) or
105/// [ATX](https://spec.commonmark.org/0.31.2/#atx-headings). Level 3 and
106/// higher headings always use ATX headings, since Markdown only supports
107/// setext headers for levels 1 and 2.
108#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
109#[serde(rename_all = "camelCase")]
110pub enum HeadingKind {
111  /// Uses an underline of `=` or `-` beneath the heading text for level 1 and
112  /// 2 headings.
113  Setext,
114  /// Uses `#` or `##` before the heading text for level 1 and 2 headings.
115  Atx,
116}
117
118generate_str_to_from![HeadingKind, [Setext, "setext"], [Atx, "atx"]];
119
120/// The style of indentation to use for list items.
121///
122/// CommonMark aligns continuation lines to the content column after the marker
123/// (e.g. 3 spaces for `1. `, 4 spaces for `10. `). PythonMarkdown uses a fixed
124/// 4-space indent regardless of marker width, which is required by tools like
125/// mkdocs-material.
126#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
127#[serde(rename_all = "camelCase")]
128pub enum ListIndentKind {
129  /// Indents continuation lines to align with the content after the list marker (default).
130  CommonMark,
131  /// Always indents by 4 spaces, regardless of marker width.
132  PythonMarkdown,
133}
134
135generate_str_to_from![ListIndentKind, [CommonMark, "commonMark"], [PythonMarkdown, "pythonMarkdown"]];