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 ignore_directive: String,
20  pub ignore_file_directive: String,
21  pub ignore_start_directive: String,
22  pub ignore_end_directive: String,
23  /// Custom tag to file extension mappings for formatting code blocks.
24  /// For example: { "custom-tag": "md" }
25  #[serde(default)]
26  pub tags: HashMap<String, String>,
27}
28
29/// Text wrapping possibilities.
30#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
31#[serde(rename_all = "camelCase")]
32pub enum TextWrap {
33  /// Always wraps text.
34  Always,
35  /// Maintains line breaks (default).
36  Maintain,
37  /// Never wraps text.
38  Never,
39}
40
41generate_str_to_from![TextWrap, [Always, "always"], [Maintain, "maintain"], [Never, "never"]];
42
43/// The character to use for emphasis/italics.
44#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
45#[serde(rename_all = "camelCase")]
46pub enum EmphasisKind {
47  /// Uses asterisks (*) for emphasis.
48  Asterisks,
49  /// Uses underscores (_) for emphasis (default).
50  Underscores,
51}
52
53generate_str_to_from![EmphasisKind, [Asterisks, "asterisks"], [Underscores, "underscores"]];
54
55/// The character to use for strong emphasis/bold.
56#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
57#[serde(rename_all = "camelCase")]
58pub enum StrongKind {
59  /// Uses asterisks (**) for strong emphasis (default).
60  Asterisks,
61  /// Uses underscores (__) for strong emphasis.
62  Underscores,
63}
64
65generate_str_to_from![StrongKind, [Asterisks, "asterisks"], [Underscores, "underscores"]];
66
67/// The character to use primarily for lists.
68///
69/// Unnumbered lists will be formatted to use a common list character, i.e., the primary list
70/// character. Additionally, an alternate list character is used to separate lists which are not
71/// separated by other paragraphs. This parameter defines which character should be used as primary
72/// list character, i.e., either '-' (default) or '*'. The alternate list character will be the one
73/// which is _not_ primary.
74#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
75#[serde(rename_all = "camelCase")]
76pub enum UnorderedListKind {
77  /// Uses dashes (-) as primary character for lists (default).
78  ///
79  /// In this case, asterisks are used as alternate list characters.
80  Dashes,
81  /// Uses asterisks (*) as primary character for lists.
82  ///
83  /// In this case, dashes are used as alternate list characters.
84  Asterisks,
85}
86
87impl UnorderedListKind {
88  /// Determine the character to use for a list, i.e., '-' or '*'.
89  ///
90  /// The result depends on the configuration and whether the primary or alternate character is
91  /// requested. See [`Self`].
92  pub fn list_char(&self, is_alternate: bool) -> char {
93    match (self, is_alternate) {
94      (Self::Dashes, true) | (Self::Asterisks, false) => '*',
95      _ => '-',
96    }
97  }
98}
99
100generate_str_to_from![UnorderedListKind, [Dashes, "dashes"], [Asterisks, "asterisks"]];
101
102/// The style of heading to use for level 1 and level 2 headings:
103/// [setext](https://spec.commonmark.org/0.31.2/#setext-headings) or
104/// [ATX](https://spec.commonmark.org/0.31.2/#atx-headings). Level 3 and
105/// higher headings always use ATX headings, since Markdown only supports
106/// setext headers for levels 1 and 2.
107#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
108#[serde(rename_all = "camelCase")]
109pub enum HeadingKind {
110  /// Uses an underline of `=` or `-` beneath the heading text for level 1 and
111  /// 2 headings.
112  Setext,
113  /// Uses `#` or `##` before the heading text for level 1 and 2 headings.
114  Atx,
115}
116
117generate_str_to_from![HeadingKind, [Setext, "setext"], [Atx, "atx"]];