Skip to content

Commit 60fd6d4

Browse files
authored
Fix to avoid change comma in Markdown frontmater tags. (#294)
Closes #292
1 parent 1d75ab8 commit 60fd6d4

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

autocorrect/grammar/markdown.pest

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ line = _{ expr | newline }
44
expr = _{
55
comment
66
| html
7-
| meta_info
7+
| front_matter
88
| block
99
| inline
1010
| td_tag
@@ -21,8 +21,8 @@ CJK = {
2121

2222
/// Block elements, e.g.: paragraphs, lists, code blocks, etc.
2323
block = ${
24-
meta_tags
25-
| hr
24+
hr
25+
| meta_tags
2626
| list
2727
| codeblock
2828
| block_item
@@ -83,18 +83,20 @@ list_prefix = @{
8383
/// author: Jason Lee
8484
/// tags: Rust, JavaScript
8585
/// ------------------
86-
meta_info = ${ meta_wrap ~ newline ~ meta_pair* ~ meta_wrap ~ newline* }
87-
meta_wrap = @{ "-"{3, } }
88-
meta_pair = ${ meta_key ~ string ~ newline }
89-
meta_key = @{ (!(":" | newline) ~ identifier)* ~ ":" ~ " "* }
86+
front_matter = ${ front_matter_tag ~ newline ~ (meta_tags | meta_pair)* ~ front_matter_tag ~ newline* }
87+
front_matter_tag = @{ "-"{3, } }
88+
meta_pair = ${ meta_key ~ string ~ newline }
89+
meta_key = @{ (!(":" | newline) ~ identifier)* ~ ":" ~ " "* }
9090

9191
/// Ignore tags in Markdown, e.g.: "tags: 美国, 中国"
9292
meta_tags = @{ meta_key ~ meta_tags_val+ ~ meta_tags_item ~ newline }
9393
meta_tags_val = @{ meta_tags_item ~ meta_tags_divider }
9494
meta_tags_item = { (!(newline | ",") ~ (CJK | " " | ASCII_ALPHANUMERIC))* }
9595
meta_tags_divider = { " "* ~ "," ~ " "* }
96-
image_prefix = @{ "!" }
97-
img = ${ image_prefix ~ (wikilinks | link) }
96+
97+
/// Matches image link, e.g.: `![Alt text](/path/to/img.jpg)` or `![[ImageName]]`
98+
image_prefix = @{ "!" }
99+
img = ${ image_prefix ~ (wikilinks | link) }
98100

99101
/// Matches link, e.g.: `[Hello](/hello)` or `[Hello]`
100102
link = ${ link_string_wrap ~ href? }
@@ -123,4 +125,4 @@ close_comment = @{ "-->" }
123125
paren = { open_paren ~ inner_paren ~ paren* ~ inner_paren* ~ close_paren | open_paren ~ close_paren }
124126
inner_paren = { (!(newline | open_paren | close_paren) ~ ANY)+ }
125127
open_paren = { "(" }
126-
close_paren = { ")" }
128+
close_paren = { ")" }

autocorrect/src/code/markdown.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,31 @@ mod tests {
1717
use indoc::indoc;
1818
use pretty_assertions::assert_eq;
1919

20+
#[test]
21+
fn test_frontmatter() {
22+
crate::config::setup_test();
23+
24+
let example = indoc! {r###"
25+
---
26+
title: 示例标题Title
27+
tags: 测试, 示例
28+
---
29+
30+
这是正文content,包含一些test文本。
31+
"###};
32+
33+
let expected = indoc! {r###"
34+
---
35+
title: 示例标题 Title
36+
tags: 测试, 示例
37+
---
38+
39+
这是正文 content,包含一些 test 文本。
40+
"###};
41+
42+
assert_eq!(expected, format_for(example, "markdown").to_string());
43+
}
44+
2045
#[test]
2146
fn test_format_markdown() {
2247
crate::config::setup_test();
@@ -28,6 +53,7 @@ mod tests {
2853
Slug: /appstore/ipad_and_ios
2954
user.name: Jason
3055
original_slug: Web/CSS/网格-模板-列
56+
tags: 模版, 网格, CSS
3157
---
3258
3359
This_Page-Tags: 美国, 中国,德国 , France 法国
@@ -169,6 +195,7 @@ mod tests {
169195
Slug: /appstore/ipad_and_ios
170196
user.name: Jason
171197
original_slug: Web/CSS/网格-模板-列
198+
tags: 模版, 网格, CSS
172199
---
173200
174201
This_Page-Tags: 美国, 中国,德国 , France 法国

0 commit comments

Comments
 (0)