Skip to content

Commit

Permalink
Fix Markdown parse to recognize meta_tags and normal paragraph.
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Dec 23, 2022
1 parent 53f0b7c commit 2022443
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
4 changes: 2 additions & 2 deletions autocorrect/Cargo.toml
Expand Up @@ -20,8 +20,8 @@ diff = "0.1.13"
ignore = "0.4"
lazy_static = "1.4.0"
owo-colors = "3"
pest = "2.4.0"
pest_derive = "2.4.0"
pest = "2.5.2"
pest_derive = "2.5.2"
regex = "1"
serde = {version = "1", features = ["derive"]}
serde_json = "1.0.83"
Expand Down
11 changes: 7 additions & 4 deletions autocorrect/grammar/markdown.pest
Expand Up @@ -5,6 +5,8 @@ expr = _{
comment | html_tag | meta_info | block | inline | td_tag
}

CJK = { HAN | HANGUL | KATAKANA | HIRAGANA }

block = ${
meta_tags
| list
Expand All @@ -17,6 +19,7 @@ paragraph = { (string | inline)+ }

html_tag = @{ "<" ~ "/"? ~ (!(">") ~ ANY)* ~ ">" }

IDENT = @{ ("_" | "-" | "." | ASCII_ALPHANUMERIC) }

newline = ${ "\n" | "\r" }
space = @{ " "* }
Expand All @@ -29,7 +32,7 @@ codeblock = ${
indent_code = @{
indent ~ (!"\n" ~ ANY)* ~ newline
}
codeblock_lang = { ASCII_ALPHA* }
codeblock_lang = { IDENT* }
codeblock_code = { (!(PEEK) ~ ANY)* }
td_tag = @{ space ~ "|" ~ space }

Expand Down Expand Up @@ -63,13 +66,13 @@ list_prefix = @{
meta_info = ${ meta_wrap ~ newline ~ meta_pair* ~ meta_wrap ~ newline* }
meta_wrap = @{ "-"{3,} }
meta_pair = ${ meta_key ~ string ~ newline }
meta_key = @{ (!(":" | newline) ~ ANY)* ~ ":" ~ " "* }
meta_key = @{ (!(":" | newline) ~ IDENT)* ~ ":" ~ " "* }

// Ignore tags in Markdown
// tags: 美国, 中国
meta_tags = @{ meta_key ~ meta_tags_val+ ~ meta_tags_item }
meta_tags = @{ meta_key ~ meta_tags_val+ ~ meta_tags_item ~ NEWLINE }
meta_tags_val = @{ meta_tags_item ~ meta_tags_divider }
meta_tags_item = { (!(newline | ",") ~ ANY)* }
meta_tags_item = { (!(newline | ",") ~ (CJK | ASCII_ALPHANUMERIC))* }
meta_tags_divider = { " "* ~ "," ~ " "* }

image_prefix = @{ "!" }
Expand Down
22 changes: 16 additions & 6 deletions autocorrect/src/code/markdown.rs
Expand Up @@ -21,12 +21,15 @@ mod tests {
let example = r###"
---
title: iPad 和 iOS 接入的不同点
id: h
slug: /appstore/ipad_and_ios
post-id: h
Slug: /appstore/ipad_and_ios
user.name: Jason
original_slug: Web/CSS/网格-模板-列
---
page-tags: 美国, 中国,德国 , 法国
This_Page-Tags: 美国, 中国,德国 , France
Example: Hello你好,世界.
# 这是Heading 1大标题[示例](#示例),代码内部:`minmax(最小值,最大值10)`不应该改变。
Expand Down Expand Up @@ -139,17 +142,22 @@ let a = "你好hello";
- [link链接](https://google.com/a/b/url不处理)
- Escher puzzle([链接](https://google.com))
- 一个[[Wikilinks测试]]示例
请记住:对该仓库的贡献,应遵循我们的GitHub社区准则。
"###;

let expected = r###"
---
title: iPad 和 iOS 接入的不同点
id: h
slug: /appstore/ipad_and_ios
post-id: h
Slug: /appstore/ipad_and_ios
user.name: Jason
original_slug: Web/CSS/网格-模板-列
---
page-tags: 美国, 中国,德国 , 法国
This_Page-Tags: 美国, 中国,德国 , France
Example: Hello 你好,世界。
# 这是 Heading 1 大标题[示例](#示例),代码内部:`minmax(最小值,最大值10)`不应该改变。
Expand Down Expand Up @@ -262,6 +270,8 @@ let a = "你好 hello";
- [link 链接](https://google.com/a/b/url不处理)
- Escher puzzle([链接](https://google.com))
- 一个[[Wikilinks测试]]示例
请记住:对该仓库的贡献,应遵循我们的 GitHub 社区准则。
"###;

assert_eq!(expected, format_for(example, "markdown").to_string());
Expand Down
2 changes: 1 addition & 1 deletion autocorrect/src/format.rs
Expand Up @@ -99,7 +99,7 @@ mod tests {
"(路透社)-预计全年净亏损约1.3亿港元*预期因出售汽车" => "(路透社)- 预计全年净亏损约 1.3 亿港元*预期因出售汽车",
"(路透社)-预计全年净亏损约1.3亿\n\n港元*预期因出售汽车" => "(路透社)- 预计全年净亏损约 1.3 亿\n\n港元*预期因出售汽车",
"Cell或RefCell类型使用某种形式的*内部" => "Cell 或 RefCell 类型使用某种形式的*内部",
"Cell或RefCell类型使用某种形式的*内部可变性*" => "Cell 或 RefCell 类型使用某种形式的*内部可变性*",
"Cell或RefCell类型使用某种形式的*内部可变性*" => "Cell 或 RefCell 类型使用某种形式的*内部可变性*"
];

assert_cases(cases);
Expand Down

0 comments on commit 2022443

Please sign in to comment.