Skip to content
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

Type link: Fixed ending punctuation to recognise links. Closes #14497. #16264

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

spicyMcCrispy
Copy link

@spicyMcCrispy spicyMcCrispy commented Apr 22, 2024

Made it so that when typing, including copy and paste, typing ending punctuations will have the link be recognised as a link. Added only for ending so parameters using ? are not affected.

Type: Message. Closes #14497

Summary by CodeRabbit

  • New Features
    • Enhanced link detection in text to include punctuation marks such as period, exclamation mark, colon, comma, semicolon, and question mark.
    • Added support for special characters as alternatives to space for creating links.
    • Improved test coverage for various scenarios involving text links and punctuation marks.
    • Introduced a new test scenario for checking link creation with ending punctuation.

Made it so that when typing, including copy and paste, typing ending punctuations will have the link be recognised as a link. Added only for ending so parameters using ? are not affected.
Copy link
Contributor

coderabbitai bot commented Apr 22, 2024

Walkthrough

The update enhances the AutoLink plugin in CKEditor to handle trailing punctuation more effectively. It ensures that punctuation marks like periods, commas, etc., are not mistakenly included in auto-generated URLs.

Changes

File Path Change Summary
packages/ckeditor5-link/src/autolink.ts Updated link detection to include additional punctuation checks at the end of text for creating links. Added support for special characters as alternatives to space for link creation. Improved isSingleSpaceAtTheEnd function to consider various punctuation marks.
packages/ckeditor5-link/tests/autolink.js, packages/ckeditor5-link/tests/manual/autolink.md Added tests for link creation with different punctuation marks to expand test coverage.

Assessment against linked issues

Objective Addressed Explanation
Autolink should not include trailing punctuation (#14497)

Recent Review Details

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between f0c6e42 and c7934c2.
Files selected for processing (1)
  • packages/ckeditor5-link/src/autolink.ts (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/ckeditor5-link/src/autolink.ts

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Comment on lines 338 to 346
function isSingleSpaceOrPunctuationAtTheEnd( text: string ): boolean {
return ( text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === ' ' ||
text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === '.' ||
text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === '!' ||
text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === ':' ||
text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === ',' ||
text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === ';' ||
text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === '?'
) && text[ text.length - 2 ] !== ' ';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor the function for improved readability and performance.

- function isSingleSpaceOrPunctuationAtTheEnd( text: string ): boolean {
-     return ( text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === ' ' ||
-             text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === '.' ||
-             text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === '!' ||
-             text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === ':' ||
-             text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === ',' ||
-             text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === ';' ||
-             text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === '?'
-     ) && text[ text.length - 2 ] !== ' ';
- }
+ function isSingleSpaceOrPunctuationAtTheEnd( text: string ): boolean {
+     const lastChar = text[ text.length - 1 ];
+     const validPunctuation = ' .!:,;?'.includes(lastChar);
+     return text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && validPunctuation && text[ text.length - 2 ] !== ' ';
+ }

This refactor simplifies the function by using a single string to check for valid punctuation marks, improving both readability and performance by reducing the number of conditions checked.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
function isSingleSpaceOrPunctuationAtTheEnd( text: string ): boolean {
return ( text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === ' ' ||
text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === '.' ||
text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === '!' ||
text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === ':' ||
text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === ',' ||
text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === ';' ||
text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === '?'
) && text[ text.length - 2 ] !== ' ';
function isSingleSpaceOrPunctuationAtTheEnd( text: string ): boolean {
const lastChar = text[ text.length - 1 ];
const validPunctuation = ' .!:,;?'.includes(lastChar);
return text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && validPunctuation && text[ text.length - 2 ] !== ' ';
}

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

Actionable comments outside the diff hunks (3)
packages/ckeditor5-link/tests/manual/autolink.md (3)

Line range hint 1-1: Correct the spelling mistake in "AutoLink feature".

- AutoLink feature
+ AutoLink Feature

Line range hint 1-9: Ensure all list items start with a capital letter.

- staring with `http://`.
- staring with `https://`.
- staring without a protocol (www.cksource.com).
+ Starting with `http://`.
+ Starting with `https://`.
+ Starting without a protocol (www.cksource.com).

Line range hint 32-32: Use underscores for emphasis instead of asterisks to maintain consistency.

- Check if *only* created link was removed:
+ Check if _only_ created link was removed:

1. A Period (`.`)
2. An exclamation mark (`!`)
3. A Colon (`:`)
4. A comma (`,`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a space after the comma for better readability.

- 4. A comma (`,`)
+ 4. A comma (`, `)

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
4. A comma (`,`)
4. A comma (`, `)

@@ -10,6 +10,15 @@
2. Type space after a URL.
3. Check if text typed before space get converted to link.

#### _Special characters_
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a blank line above the heading for better markdown formatting.

+ 
#### _Special characters_

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
#### _Special characters_
#### _Special characters_

Comment on lines +15 to +21
1. A Period (`.`)
2. An exclamation mark (`!`)
3. A Colon (`:`)
4. A comma (`,`)
5. A Semicolon (`;`)
6. A Question mark (`?`)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surround the list with blank lines for proper markdown formatting.

+ 
1. A Period (`.`)
2. An exclamation mark (`!`)
3. A Colon (`:`)
4. A comma (`,`)
5. A Semicolon (`;`)
6. A Question mark (`?`)
+ 

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
1. A Period (`.`)
2. An exclamation mark (`!`)
3. A Colon (`:`)
4. A comma (`,`)
5. A Semicolon (`;`)
6. A Question mark (`?`)
1. A Period (`.`)
2. An exclamation mark (`!`)
3. A Colon (`:`)
4. A comma (`,`)
5. A Semicolon (`;`)
6. A Question mark (`?`)

Comment on lines 252 to 306
it( 'adds linkHref attribute to a text link after space( )', () => {
simulateTyping( 'https://www.cksource.com ' );

expect( getData( model ) ).to.equal(
'<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text> []</paragraph>'
);
} );

it( 'adds linkHref attribute to a text link after period(.)', () => {
simulateTyping( 'https://www.cksource.com.' );

expect( getData( model ) ).to.equal(
'<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>.[]</paragraph>'
);
} );

it( 'adds linkHref attribute to a text link after exclamation mark(!)', () => {
simulateTyping( 'https://www.cksource.com.' );

expect( getData( model ) ).to.equal(
'<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>![]</paragraph>'
);
} );

it( 'adds linkHref attribute to a text link after colon(:)', () => {
simulateTyping( 'https://www.cksource.com.' );

expect( getData( model ) ).to.equal(
'<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>:[]</paragraph>'
);
} );

it( 'adds linkHref attribute to a text link after comma(,)', () => {
simulateTyping( 'https://www.cksource.com.' );

expect( getData( model ) ).to.equal(
'<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>,[]</paragraph>'
);
} );

it( 'adds linkHref attribute to a text link after semi-colon(;)', () => {
simulateTyping( 'https://www.cksource.com.' );

expect( getData( model ) ).to.equal(
'<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>;[]</paragraph>'
);
} );

it( 'adds linkHref attribute to a text link after question mark(?)', () => {
simulateTyping( 'https://www.cksource.com.' );

expect( getData( model ) ).to.equal(
'<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>?[]</paragraph>'
);
} );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure the correct punctuation mark is used in each test case.

- simulateTyping( 'https://www.cksource.com.' );
+ simulateTyping( 'https://www.cksource.com' );

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
it( 'adds linkHref attribute to a text link after space( )', () => {
simulateTyping( 'https://www.cksource.com ' );
expect( getData( model ) ).to.equal(
'<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text> []</paragraph>'
);
} );
it( 'adds linkHref attribute to a text link after period(.)', () => {
simulateTyping( 'https://www.cksource.com.' );
expect( getData( model ) ).to.equal(
'<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>.[]</paragraph>'
);
} );
it( 'adds linkHref attribute to a text link after exclamation mark(!)', () => {
simulateTyping( 'https://www.cksource.com.' );
expect( getData( model ) ).to.equal(
'<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>![]</paragraph>'
);
} );
it( 'adds linkHref attribute to a text link after colon(:)', () => {
simulateTyping( 'https://www.cksource.com.' );
expect( getData( model ) ).to.equal(
'<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>:[]</paragraph>'
);
} );
it( 'adds linkHref attribute to a text link after comma(,)', () => {
simulateTyping( 'https://www.cksource.com.' );
expect( getData( model ) ).to.equal(
'<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>,[]</paragraph>'
);
} );
it( 'adds linkHref attribute to a text link after semi-colon(;)', () => {
simulateTyping( 'https://www.cksource.com.' );
expect( getData( model ) ).to.equal(
'<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>;[]</paragraph>'
);
} );
it( 'adds linkHref attribute to a text link after question mark(?)', () => {
simulateTyping( 'https://www.cksource.com.' );
expect( getData( model ) ).to.equal(
'<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>?[]</paragraph>'
);
} );
it( 'adds linkHref attribute to a text link after space( )', () => {
simulateTyping( 'https://www.cksource.com ' );
expect( getData( model ) ).to.equal(
'<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text> []</paragraph>'
);
} );
it( 'adds linkHref attribute to a text link after period(.)', () => {
simulateTyping( 'https://www.cksource.com.' );
expect( getData( model ) ).to.equal(
'<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>.[]</paragraph>'
);
} );
it( 'adds linkHref attribute to a text link after exclamation mark(!)', () => {
simulateTyping( 'https://www.cksource.com!' );
expect( getData( model ) ).to.equal(
'<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>![]</paragraph>'
);
} );
it( 'adds linkHref attribute to a text link after colon(:)', () => {
simulateTyping( 'https://www.cksource.com:' );
expect( getData( model ) ).to.equal(
'<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>:[]</paragraph>'
);
} );
it( 'adds linkHref attribute to a text link after comma(,)', () => {
simulateTyping( 'https://www.cksource.com,' );
expect( getData( model ) ).to.equal(
'<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>,[]</paragraph>'
);
} );
it( 'adds linkHref attribute to a text link after semi-colon(;)', () => {
simulateTyping( 'https://www.cksource.com;' );
expect( getData( model ) ).to.equal(
'<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>;[]</paragraph>'
);
} );
it( 'adds linkHref attribute to a text link after question mark(?)', () => {
simulateTyping( 'https://www.cksource.com?' );
expect( getData( model ) ).to.equal(
'<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>?[]</paragraph>'
);
} );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Autolink should not include trailing punctuation
2 participants