-
Notifications
You must be signed in to change notification settings - Fork 40
feat(Link): do not insert http:// for links from buffer
#1043
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
saxumcordis
wants to merge
2
commits into
main
Choose a base branch
from
save-no-href
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/editor/src/extensions/markdown/Link/linkifyRawHrefPlugin.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import MarkdownIt from 'markdown-it'; | ||
|
|
||
| import {linkifyRawHrefPlugin} from './linkifyRawHrefPlugin'; | ||
|
|
||
| function mdWithPlugin() { | ||
| const md = MarkdownIt({linkify: true}); | ||
| linkifyRawHrefPlugin(md); | ||
| return md; | ||
| } | ||
|
|
||
| describe('linkifyRawHrefPlugin', () => { | ||
| it('uses raw hostname in href for pasted text with trailing words', () => { | ||
| const md = mdWithPlugin(); | ||
| expect(md.renderInline('ya.ru dasda')).toBe('<a href="ya.ru">ya.ru</a> dasda'); | ||
| }); | ||
|
|
||
| it('keeps explicit scheme in href', () => { | ||
| const md = mdWithPlugin(); | ||
| expect(md.renderInline('https://ya.ru x')).toBe( | ||
| '<a href="https://ya.ru">https://ya.ru</a> x', | ||
| ); | ||
| }); | ||
|
|
||
| it('keeps mailto href for emails', () => { | ||
| const md = mdWithPlugin(); | ||
| expect(md.renderInline('a@b.co rest')).toBe('<a href="mailto:a@b.co">a@b.co</a> rest'); | ||
| }); | ||
| }); | ||
46 changes: 46 additions & 0 deletions
46
packages/editor/src/extensions/markdown/Link/linkifyRawHrefPlugin.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import type MarkdownIt from 'markdown-it'; | ||
|
|
||
| /** | ||
| * markdown-it linkify sets href to linkify-it's normalized URL (e.g. http://ya.ru for "ya.ru"). | ||
| * Use the matched raw string when there was no explicit schema so href matches pasted text. | ||
| */ | ||
| export function linkifyRawHrefPlugin(md: MarkdownIt): MarkdownIt { | ||
| md.core.ruler.after('linkify', 'linkify_raw_href', (state) => { | ||
| for (let j = 0; j < state.tokens.length; j++) { | ||
| const block = state.tokens[j]; | ||
| if (block.type !== 'inline' || !block.children) continue; | ||
|
|
||
| const children = block.children; | ||
| for (let i = 0; i < children.length; i++) { | ||
| const open = children[i]; | ||
| if ( | ||
| open.type !== 'link_open' || | ||
| open.markup !== 'linkify' || | ||
| open.info !== 'auto' | ||
| ) { | ||
| continue; | ||
| } | ||
|
|
||
| const textTok = children[i + 1]; | ||
| const close = children[i + 2]; | ||
| if (!textTok || textTok.type !== 'text' || !close || close.type !== 'link_close') { | ||
| continue; | ||
| } | ||
|
|
||
| const chunk = textTok.content; | ||
| const matches = md.linkify.match(chunk); | ||
| if (!matches || matches.length !== 1) continue; | ||
|
|
||
| const m = matches[0]; | ||
| if (m.index !== 0 || m.lastIndex !== chunk.length) continue; | ||
|
|
||
| const source = m.schema ? m.url : m.raw; | ||
| const href = md.normalizeLink(source); | ||
| if (md.validateLink(href)) { | ||
| open.attrSet('href', href); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| return md; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,10 +27,11 @@ const { | |
| }).build(); | ||
| plugins.unshift(LoggerFacet.of(logger)); | ||
|
|
||
| const {doc, p, lnk} = builders<'doc' | 'p' | 'lnk'>(schema, { | ||
| const {doc, p, lnk, lnkYa} = builders<'doc' | 'p' | 'lnk' | 'lnkYa'>(schema, { | ||
| doc: {nodeType: BaseNode.Doc}, | ||
| p: {nodeType: BaseNode.Paragraph}, | ||
| lnk: {markType: linkMarkName, [LinkAttr.Href]: 'http://example.com?'}, | ||
| lnkYa: {markType: linkMarkName, [LinkAttr.Href]: 'ya.ru'}, | ||
| }); | ||
|
|
||
| const {same} = createMarkupChecker({parser, serializer}); | ||
|
|
@@ -41,6 +42,20 @@ describe('link paste plugin', () => { | |
| expect(match?.[0]?.raw).toBe('http://example.com'); | ||
| }); | ||
|
|
||
| it('pastes bare hostname without adding scheme to href', () => { | ||
| const startDoc = doc(p('<a>')); | ||
| const state = EditorState.create({ | ||
| schema, | ||
| doc: startDoc, | ||
| selection: TextSelection.create(startDoc, startDoc.tag.a), | ||
| plugins, | ||
| }); | ||
| const view = new EditorView(null, {state}); | ||
| dispatchPasteEvent(view, {'text/plain': 'ya.ru'}); | ||
| expect(view.state.doc).toMatchNode(doc(p(lnkYa('ya.ru')))); | ||
| same('[ya.ru](ya.ru)', view.state.doc); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, when inserting a link, it will be inserted with the protocol. After this fix, judging by the test, there won't be any protocol. I suggest changing it to |
||
| }); | ||
|
|
||
| it('pastes url ending with question mark as link for selected text', () => { | ||
| const startDoc = doc(p('<a>test text<b>')); | ||
| const state = EditorState.create({ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand correctly, the
linkifyRawHrefPluginwas added to avoid adding the protocol when the user hasn't inserted it. In my opinion, the correct approach is to always use the protocol in the link itself, as before, but not display it in the text.So, the plugin seems unnecessary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if i paste fdqn for ssh?