Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit805e506

Browse files
author
mydearxym
committed
feat(editor-js): add mention parser
1 parent26fd8ed commit805e506

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
defmoduleHelper.Converter.MentionParserdo
2+
@moduledoc"""
3+
parse mention(@xx) from given string
4+
see https://gitgud.io/lambadalambda/pleroma/commit/2ab1d915e3a7db329f09332e8b688e4bd405b748
5+
"""
6+
7+
# @mention_regex ~r/@[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@?[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*/u
8+
# see: https://github.com/regexhq/mentions-regex/blob/master/index.js#L21
9+
# in elixir regex shoud replace \ with \\
10+
# see http://developerworks.github.io/2015/01/02/elixir-regex/
11+
@mention_regex~r/(?:^|[^a-zA-Z0-9_@!@#$%&*])(?:(?:@|@)(?!\/))([a-zA-Z0-9\/_.]{1,15})(?:\b(?!@|@)|$)/
12+
13+
@doc"""
14+
parse mention(@xx) from given string
15+
"""
16+
defparse(text,:mentions)do
17+
Regex.scan(@mention_regex,text)
18+
|>List.flatten()
19+
|>Enum.map(&String.trim(&1))
20+
|>Enum.filter(&String.starts_with?(&1,"@"))
21+
|>Enum.uniq()
22+
end
23+
end

‎test/helper/converter/md_to_editor_test.exs‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
defmoduleGroupherServer.Test.Helper.Converter.MdToEditordo
2+
@moduledoc"""
3+
parse markdown string to editorjs's json format
4+
"""
25
useGroupherServerWeb.ConnCase,async:true
36

47
aliasHelper.Converter.MdToEditor,as:Converter
58

69
describe"[basic md test]"do
7-
@tag:wip
810
test"ast parser should work"do
911
mdstring="""
1012
# header one
@@ -126,7 +128,6 @@ defmodule GroupherServer.Test.Helper.Converter.MdToEditor do
126128
]
127129
end
128130

129-
@tag:wip2
130131
test"compose multi inline style in list-item should work"do
131132
mdstring="""
132133
- this is ul **list** item 1
@@ -149,7 +150,6 @@ defmodule GroupherServer.Test.Helper.Converter.MdToEditor do
149150
]
150151
end
151152

152-
@tag:wip2
153153
test"complex nested markdown rules should not raise error"do
154154
mdstring="""
155155
this is a paragraph with **_`inline-code-string`*__* in it
@@ -180,7 +180,6 @@ defmodule GroupherServer.Test.Helper.Converter.MdToEditor do
180180
]
181181
end
182182

183-
@tag:wip2
184183
test"nested inline code should parse right"do
185184
mdstring="""
186185
this is a paragraph with **`inline-code-string`** in it
@@ -229,7 +228,6 @@ defmodule GroupherServer.Test.Helper.Converter.MdToEditor do
229228
]
230229
end
231230

232-
@tag:wip2
233231
test"complex ast parser should work"do
234232
mdstring="""
235233
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
defmoduleGroupherServer.Test.Helper.Converter.MentionParserdo
2+
@moduledoc"""
3+
parse mention(@) from given string
4+
"""
5+
useGroupherServerWeb.ConnCase,async:true
6+
7+
aliasHelper.Converter.MentionParser
8+
9+
@test_message~S"""
10+
@you http://example.com/test?a=1&b=abc+123#abc
11+
this is a #test #message with #a few #test tags from +me @you and @中文我 xxx@email.com
12+
13+
this is a #test #message with #a few #test tags from +me @you2 and @中文我 xxx@email.com
14+
"""
15+
16+
@tag:wip
17+
test"parse should return an empty for blank input"do
18+
ret=MentionParser.parse("",:mentions)
19+
20+
assertret==[]
21+
end
22+
23+
@tag:wip
24+
test"mention should parsed in list"do
25+
ret=MentionParser.parse(@test_message,:mentions)
26+
27+
assertret==["@you","@you2"]
28+
end
29+
30+
@tag:wip
31+
test"email should not be parsed"do
32+
ret=MentionParser.parse(@test_message,:mentions)
33+
34+
assert"xxx@email.com"not inret
35+
assert"@email"not inret
36+
assert"@email.com"not inret
37+
end
38+
end

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp