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
0
Merge Requests
0
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
Léo-Paul Géneau
gitlab-ce
Commits
86177284
Commit
86177284
authored
Oct 24, 2018
by
Johann Hubert Sonntagbauer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Link button in markdown editor recognize URLs
parent
3e18ac0e
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
49 additions
and
0 deletions
+49
-0
app/assets/javascripts/lib/utils/text_markdown.js
app/assets/javascripts/lib/utils/text_markdown.js
+17
-0
changelogs/unreleased/52115-Link-button-in-markdown-editor-should-recognize-URLs.yml
...-Link-button-in-markdown-editor-should-recognize-URLs.yml
+5
-0
spec/javascripts/lib/utils/text_markdown_spec.js
spec/javascripts/lib/utils/text_markdown_spec.js
+27
-0
No files found.
app/assets/javascripts/lib/utils/text_markdown.js
View file @
86177284
...
@@ -2,6 +2,8 @@
...
@@ -2,6 +2,8 @@
import
$
from
'
jquery
'
;
import
$
from
'
jquery
'
;
import
{
insertText
}
from
'
~/lib/utils/common_utils
'
;
import
{
insertText
}
from
'
~/lib/utils/common_utils
'
;
const
LINK_TAG_PATTERN
=
'
[{text}](url)
'
;
function
selectedText
(
text
,
textarea
)
{
function
selectedText
(
text
,
textarea
)
{
return
text
.
substring
(
textarea
.
selectionStart
,
textarea
.
selectionEnd
);
return
text
.
substring
(
textarea
.
selectionStart
,
textarea
.
selectionEnd
);
}
}
...
@@ -76,6 +78,21 @@ export function insertMarkdownText({ textArea, text, tag, blockTag, selected, wr
...
@@ -76,6 +78,21 @@ export function insertMarkdownText({ textArea, text, tag, blockTag, selected, wr
removedFirstNewLine
=
false
;
removedFirstNewLine
=
false
;
currentLineEmpty
=
false
;
currentLineEmpty
=
false
;
// check for link pattern and selected text is an URL
// if so fill in the url part instead of the text part of the pattern.
if
(
tag
===
LINK_TAG_PATTERN
)
{
if
(
URL
)
{
try
{
const
ignoredUrl
=
new
URL
(
selected
);
// valid url
tag
=
'
[text]({text})
'
;
select
=
'
text
'
;
}
catch
(
e
)
{
// ignore - no valid url
}
}
}
// Remove the first newline
// Remove the first newline
if
(
selected
.
indexOf
(
'
\n
'
)
===
0
)
{
if
(
selected
.
indexOf
(
'
\n
'
)
===
0
)
{
removedFirstNewLine
=
true
;
removedFirstNewLine
=
true
;
...
...
changelogs/unreleased/52115-Link-button-in-markdown-editor-should-recognize-URLs.yml
0 → 100644
View file @
86177284
---
title
:
Link button in markdown editor recognize URLs
merge_request
:
1983
author
:
Johann Hubert Sonntagbauer
type
:
changed
spec/javascripts/lib/utils/text_markdown_spec.js
View file @
86177284
...
@@ -166,6 +166,33 @@ describe('init markdown', () => {
...
@@ -166,6 +166,33 @@ describe('init markdown', () => {
expect
(
textArea
.
selectionStart
).
toEqual
(
expectedText
.
lastIndexOf
(
select
));
expect
(
textArea
.
selectionStart
).
toEqual
(
expectedText
.
lastIndexOf
(
select
));
expect
(
textArea
.
selectionEnd
).
toEqual
(
expectedText
.
lastIndexOf
(
select
)
+
select
.
length
);
expect
(
textArea
.
selectionEnd
).
toEqual
(
expectedText
.
lastIndexOf
(
select
)
+
select
.
length
);
});
});
it
(
'
should support selected urls
'
,
()
=>
{
const
expectedUrl
=
'
http://www.gitlab.com
'
;
const
expectedSelectionText
=
'
text
'
;
const
expectedText
=
`text [
${
expectedSelectionText
}
](
${
expectedUrl
}
) text`
;
const
initialValue
=
`text
${
expectedUrl
}
text`
;
textArea
.
value
=
initialValue
;
const
selectedIndex
=
initialValue
.
indexOf
(
expectedUrl
);
textArea
.
setSelectionRange
(
selectedIndex
,
selectedIndex
+
expectedUrl
.
length
);
insertMarkdownText
({
textArea
,
text
:
textArea
.
value
,
tag
,
blockTag
:
null
,
selected
:
expectedUrl
,
wrap
:
false
,
select
,
});
expect
(
textArea
.
value
).
toEqual
(
expectedText
);
expect
(
textArea
.
selectionStart
).
toEqual
(
expectedText
.
indexOf
(
expectedSelectionText
,
1
));
expect
(
textArea
.
selectionEnd
).
toEqual
(
expectedText
.
indexOf
(
expectedSelectionText
,
1
)
+
expectedSelectionText
.
length
,
);
});
});
});
});
});
});
});
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