Skip to content

Commit

Permalink
fix: fix normalizers with incorrect html input
Browse files Browse the repository at this point in the history
  • Loading branch information
robertu7 committed Jun 14, 2024
1 parent 568198e commit d35dcb4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@matters/matters-editor",
"version": "0.2.5-alpha.6",
"version": "0.2.5-alpha.7",
"description": "Editor for matters.news",
"author": "https://github.com/thematters",
"homepage": "https://github.com/thematters/matters-editor",
Expand Down
2 changes: 1 addition & 1 deletion src/editors/extensions/mention.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export const Mention = Node.create<MentionOptions>({
'data-id': node.attrs.id,
'data-user-name': node.attrs.userName,
'data-display-name': node.attrs.displayName,
ref: 'noopener noreferrer nofollow',
rel: 'noopener noreferrer nofollow',
},
['span', `@${node.attrs.displayName ?? node.attrs.userName}`],
]
Expand Down
23 changes: 14 additions & 9 deletions src/transformers/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,32 +326,37 @@ describe('Normalization: Comment', () => {
})

test('link', () => {
const longURL =
'https://medium.com/yihan-huang-studio/%E4%BA%BA%E6%A0%BC%E6%8A%BD%E9%9B%A2%E7%9A%84%E5%B9%BB%E8%A6%BA%E6%B0%A3%E5%91%B3-%E4%BB%A5%E9%B4%89%E7%89%87%E5%85%A5%E9%A6%99%E7%9A%84-boudicca-wode-630a5b253bb3'

// normal link
expectNormalizeCommentHTML(
'<p><a target="_blank" rel="noopener noreferrer nofollow" href="https://example.com">abc</a></p>',
'<p><a target="_blank" rel="noopener noreferrer nofollow" href="https://example.com">abc</a></p>',
)

const longURL =
'https://medium.com/yihan-huang-studio/%E4%BA%BA%E6%A0%BC%E6%8A%BD%E9%9B%A2%E7%9A%84%E5%B9%BB%E8%A6%BA%E6%B0%A3%E5%91%B3-%E4%BB%A5%E9%B4%89%E7%89%87%E5%85%A5%E9%A6%99%E7%9A%84-boudicca-wode-630a5b253bb3'

// long link
expectNormalizeCommentHTML(
`<p><a target="_blank" rel="noopener noreferrer nofollow" href="${longURL}">${longURL}</a></p>`,
`<p><a target="_blank" rel="noopener noreferrer nofollow" href="${longURL}">medium.com/yihan-hua...</a></p>`,
{ truncate: { maxLength: 20, keepProtocol: false } },
)

expectNormalizeCommentHTML(
`<p><a class="mention" rel="noopener noreferrer nofollow" href="${longURL}">${longURL}</a></p>`,
`<p><a class="mention" rel="noopener noreferrer nofollow" href="${longURL}">https://medium.com/y...</a></p>`,
{ truncate: { maxLength: 20, keepProtocol: true } },
)

// long URL with protocol
expect(() =>
normalizeCommentHTML(
`<p><a target="_blank" rel="noopener noreferrer nofollow" href="${longURL}">${longURL}</a></p>`,
{ truncate: { maxLength: 0, keepProtocol: true } },
),
).toThrow('maxLength must be greater than 0')

// mention
const longDisplayName = 'displayName'.repeat(10)
expectNormalizeCommentHTML(
`<p><a class="mention" data-id="id" data-display-name="${longDisplayName}" data-user-name="userName" rel="noopener noreferrer nofollow" href="${longURL}">@${longDisplayName}</a></p>`,
`<p><a class="mention" href="/@userName" data-id="id" data-user-name="userName" data-display-name="${longDisplayName}" rel="noopener noreferrer nofollow"><span>@${longDisplayName}</span></a></p>`,
{ truncate: { maxLength: 20, keepProtocol: true } },
)
})

test('bolds is not supported', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/transformers/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const normalizeArticleHTML = (
let normalizedHtml = normalizer(html)

if (options?.truncate) {
normalizedHtml = truncateLinkText(html, options.truncate)
normalizedHtml = truncateLinkText(normalizedHtml, options.truncate)
}

return normalizedHtml
Expand All @@ -94,7 +94,7 @@ export const normalizeCommentHTML = (
let normalizedHtml = normalizer(html)

if (options?.truncate) {
normalizedHtml = truncateLinkText(html, options.truncate)
normalizedHtml = truncateLinkText(normalizedHtml, options.truncate)
}

return normalizedHtml
Expand All @@ -110,7 +110,7 @@ export const normalizeJournalHTML = (
let normalizedHtml = normalizer(html)

if (options?.truncate) {
normalizedHtml = truncateLinkText(html, options.truncate)
normalizedHtml = truncateLinkText(normalizedHtml, options.truncate)
}

return normalizedHtml
Expand Down

0 comments on commit d35dcb4

Please sign in to comment.