Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Sharkey
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Dima Krasner
Sharkey
Commits
27d0ac3d
Unverified
Commit
27d0ac3d
authored
4 years ago
by
MeiMei
Committed by
GitHub
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
In HTML to MFM, use angle bracket if needed (#6817)
parent
a8776002
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/mfm/from-html.ts
+6
-2
6 additions, 2 deletions
src/mfm/from-html.ts
src/mfm/prelude.ts
+2
-1
2 additions, 1 deletion
src/mfm/prelude.ts
test/mfm.ts
+35
-0
35 additions, 0 deletions
test/mfm.ts
with
43 additions
and
3 deletions
src/mfm/from-html.ts
+
6
−
2
View file @
27d0ac3d
import
{
parseFragment
,
DefaultTreeDocumentFragment
}
from
'
parse5
'
;
import
{
urlRegex
}
from
'
./prelude
'
;
import
{
urlRegex
Full
}
from
'
./prelude
'
;
export
function
fromHtml
(
html
:
string
,
hashtagNames
?:
string
[]):
string
{
const
dom
=
parseFragment
(
html
)
as
DefaultTreeDocumentFragment
;
...
...
@@ -54,7 +54,11 @@ export function fromHtml(html: string, hashtagNames?: string[]): string {
}
// その他
}
else
{
text
+=
(
!
href
||
(
txt
===
href
.
value
&&
txt
.
match
(
urlRegex
)))
?
txt
:
`[
${
txt
}
](
${
href
.
value
}
)`
;
text
+=
!
href
?
txt
:
txt
===
href
.
value
?
txt
.
match
(
urlRegexFull
)
?
txt
:
`<
${
txt
}
>`
:
`[
${
txt
}
](
${
href
.
value
}
)`
;
}
break
;
...
...
This diff is collapsed.
Click to expand it.
src/mfm/prelude.ts
+
2
−
1
View file @
27d0ac3d
...
...
@@ -36,4 +36,5 @@ export function createTree(type: string, children: MfmForest, props: any): MfmTr
return
T
.
createTree
({
type
,
props
},
children
);
}
export
const
urlRegex
=
/^https
?
:
\/\/[\w\/
:%#@$&?!()
\[\]
~.,=+
\-]
+/
;
export
const
urlRegex
=
/^https
?
:
\/\/[\w\/
:%#@$&?!()
\[\]
~.,=+
\-]
+/
;
export
const
urlRegexFull
=
/^https
?
:
\/\/[\w\/
:%#@$&?!()
\[\]
~.,=+
\-]
+$/
;
This diff is collapsed.
Click to expand it.
test/mfm.ts
+
35
−
0
View file @
27d0ac3d
...
...
@@ -12,6 +12,7 @@ import * as assert from 'assert';
import
{
parse
,
parsePlain
}
from
'
../src/mfm/parse
'
;
import
{
toHtml
}
from
'
../src/mfm/to-html
'
;
import
{
fromHtml
}
from
'
../src/mfm/from-html
'
;
import
{
toString
}
from
'
../src/mfm/to-string
'
;
import
{
createTree
as
tree
,
createLeaf
as
leaf
,
MfmTree
}
from
'
../src/mfm/prelude
'
;
import
{
removeOrphanedBrackets
}
from
'
../src/mfm/language
'
;
...
...
@@ -1199,3 +1200,37 @@ describe('MFM', () => {
});
});
});
describe
(
'
fromHtml
'
,
()
=>
{
it
(
'
br
'
,
()
=>
{
assert
.
deepStrictEqual
(
fromHtml
(
'
<p>abc<br><br/>d</p>
'
),
'
abc
\n\n
d
'
);
});
it
(
'
link with different text
'
,
()
=>
{
assert
.
deepStrictEqual
(
fromHtml
(
'
<p>a <a href="https://example.com/b">c</a> d</p>
'
),
'
a [c](https://example.com/b) d
'
);
});
it
(
'
link with same text
'
,
()
=>
{
assert
.
deepStrictEqual
(
fromHtml
(
'
<p>a <a href="https://example.com/b">https://example.com/b</a> d</p>
'
),
'
a https://example.com/b d
'
);
});
it
(
'
link with same text, but not encoded
'
,
()
=>
{
assert
.
deepStrictEqual
(
fromHtml
(
'
<p>a <a href="https://example.com/ä">https://example.com/ä</a> d</p>
'
),
'
a <https://example.com/ä> d
'
);
});
it
(
'
link with no url
'
,
()
=>
{
assert
.
deepStrictEqual
(
fromHtml
(
'
<p>a <a href="b">c</a> d</p>
'
),
'
a [c](b) d
'
);
});
it
(
'
link without href
'
,
()
=>
{
assert
.
deepStrictEqual
(
fromHtml
(
'
<p>a <a>c</a> d</p>
'
),
'
a c d
'
);
});
it
(
'
mention
'
,
()
=>
{
assert
.
deepStrictEqual
(
fromHtml
(
'
<p>a <a href="https://example.com/@user" class="u-url mention">@user</a> d</p>
'
),
'
a @user@example.com d
'
);
});
it
(
'
hashtag
'
,
()
=>
{
assert
.
deepStrictEqual
(
fromHtml
(
'
<p>a <a href="https://example.com/tags/a">#a</a> d</p>
'
,
[
'
#a
'
]),
'
a #a d
'
);
});
});
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment