fix(editor): should add HTTP protocol into link automatically (#11934)

Closes: [BS-3291](https://linear.app/affine-design/issue/BS-3291/工具栏展开时报错,链接无法点击打开)

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

- **New Features**
  - URLs entered without a protocol (e.g., "github.com/...") are now automatically normalized to use "https://", ensuring links are secure and consistently formatted.

- **Bug Fixes**
  - Improved handling and validation of links to prevent issues with missing or invalid protocols in bookmarks and inline links.
  - Simplified URL validation logic by leveraging native URL parsing, removing complex regex and email-specific checks.
  - Streamlined toolbar link actions to operate only on valid normalized URLs.
  - Refined URL detection in markdown preprocessing to exclude lines containing spaces from being treated as URLs.

- **Tests**
  - Added tests to verify that links without a protocol are correctly normalized and displayed across different views.
  - Updated URL validation tests to better reflect valid and invalid URL formats, including IP addresses and domain variants.

- **Style**
  - Updated snapshots to reflect the use of "https://" in links.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
fundon
2025-05-19 17:05:06 +00:00
parent 4d6a3731a3
commit fd838d4e2d
10 changed files with 160 additions and 120 deletions

View File

@@ -1271,3 +1271,43 @@ test('should display date as the original title of journal', async ({
await expect(toolbar).toBeVisible();
await expect(linkedDocTitle).toBeHidden();
});
test('should add HTTP protocol into link automatically', async ({ page }) => {
await page.keyboard.press('Enter');
await page.keyboard.type('github.com');
await page.keyboard.type('/');
await page.keyboard.type('toeverything');
await page.keyboard.type('/');
await page.keyboard.type('affine');
await page.keyboard.press('Space');
const link = 'https://github.com/toeverything/affine';
const { toolbar, switchViewBtn, cardViewBtn } = toolbarButtons(page);
const inlineLink = page.locator('affine-link');
await expect(inlineLink).toBeVisible();
let url = await inlineLink.locator('a').getAttribute('href');
expect(url).toBe(link);
await inlineLink.hover();
const linkPreview = toolbar.locator('affine-link-preview');
url = await linkPreview.locator('a').getAttribute('href');
expect(url).toBe(link);
await switchViewBtn.click();
await cardViewBtn.click();
const cardLink = page.locator('affine-bookmark');
await expect(cardLink).toBeVisible();
url = await linkPreview.locator('a').getAttribute('href');
expect(url).toBe(link);
});

View File

@@ -227,7 +227,7 @@ test(scoped`auto identify url`, async ({ page }, testInfo) => {
// set up clipboard data using html
const clipData = {
'text/plain': `test https://www.google.com`,
'text/plain': 'test https://www.google.com',
};
await waitNextFrame(page);
await page.evaluate(

View File

@@ -14,7 +14,7 @@
"delta": [
{
"attributes": {
"link": "http://affine.pro"
"link": "https://affine.pro"
},
"insert": "aaa"
}