Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
295b2ad2
Commit
295b2ad2
authored
Jun 09, 2021
by
Enrique Alcantara
Committed by
Enrique Alcántara
Jun 10, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add an input rule for creating links
parent
03664283
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
3 deletions
+49
-3
app/assets/javascripts/content_editor/extensions/link.js
app/assets/javascripts/content_editor/extensions/link.js
+19
-3
spec/frontend/content_editor/extensions/link_spec.js
spec/frontend/content_editor/extensions/link_spec.js
+30
-0
No files found.
app/assets/javascripts/content_editor/extensions/link.js
View file @
295b2ad2
import
{
markInputRule
}
from
'
@tiptap/core
'
;
import
{
Link
}
from
'
@tiptap/extension-link
'
;
import
{
defaultMarkdownSerializer
}
from
'
prosemirror-markdown/src/to_markdown
'
;
export
const
tiptapExtension
=
Link
.
configure
({
openOnClick
:
false
,
});
const
insertLinkInputRuleRegExp
=
/
(?:
^|
\s)\[([\w
|
\s
|-
]
+
)\]\(
.+
?\)
$/gm
;
export
const
tiptapExtension
=
Link
.
extend
({
addInputRules
()
{
return
[
markInputRule
(
insertLinkInputRuleRegExp
,
this
.
type
,
(
match
)
=>
{
const
extractHrefRegExp
=
/
\[([\w
|
\s
|-
]
+
)\]\((?<
href>.+
?)\)
$/gm
;
return
extractHrefRegExp
.
exec
(
match
[
0
]).
groups
;
}),
];
}
})
.
configure
({
openOnClick
:
false
,
});
export
const
serializer
=
defaultMarkdownSerializer
.
marks
.
link
;
spec/frontend/content_editor/extensions/link_spec.js
0 → 100644
View file @
295b2ad2
import
{
tiptapExtension
as
Link
}
from
'
~/content_editor/extensions/link
'
;
import
{
createTestEditor
}
from
'
../test_utils
'
;
describe
(
'
content_editor/extensions/link
'
,
()
=>
{
let
tiptapEditor
;
beforeEach
(()
=>
{
tiptapEditor
=
createTestEditor
({
extensions
:
[
Link
]
});
});
it
.
each
`
input
${
'
[gitlab](https://gitlab.com)
'
}
`
(
'
creates a link when the input rule matches $input
'
,
({
input
})
=>
{
const
{
view
}
=
tiptapEditor
;
const
{
selection
}
=
view
.
state
;
tiptapEditor
.
chain
().
insertContent
(
input
).
run
();
/**
* Calls the event handler that executes the input rule
* https://github.com/ProseMirror/prosemirror-inputrules/blob/master/src/inputrules.js#L65
* */
view
.
someProp
(
'
handleTextInput
'
,
(
f
)
=>
f
(
view
,
selection
.
from
,
selection
.
to
,
input
));
const
serializedDoc
=
tiptapEditor
.
getJSON
();
console
.
log
(
serializedDoc
.
content
[
0
].
content
[
0
])
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment