Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { expect, it } from 'vitest'
import * as Comments from '../../OCP/comments.js'
import { plainToRich } from './comments.ts'

it.for([
{ input: 'nextcloud.com', expected: 'nextcloud.com' },
Expand All @@ -28,6 +28,6 @@ it.for([
{ input: 'FirebaseInstanceId.getInstance().deleteInstanceId()', expected: 'FirebaseInstanceId.getInstance().deleteInstanceId()' },
{ input: 'I mean...it', expected: 'I mean...it' },
])('OCP.Comments should parse URLs only', ({ input, expected }) => {
const result = Comments.plainToRich(input)
const result = plainToRich(input)
expect(result).toEqual(expected)
})
41 changes: 24 additions & 17 deletions core/src/OCP/comments.js → core/src/OCP/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import $ from 'jquery'

/*
* Detects links:
* Either the http(s) protocol is given or two strings, basically limited to ascii with the last
Expand All @@ -19,23 +17,29 @@ import $ from 'jquery'
const urlRegex = /(\s|^)(https?:\/\/)([-A-Z0-9+_.]+(?::[0-9]+)?(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*)(\s|$)/ig

/**
* @param {any} content -
* Converts plain text to rich text
*
* @param content - The plain text content
*/
export function plainToRich(content) {
return this.formatLinksRich(content)
export function plainToRich(content: string) {
return formatLinksRich(content)
}

/**
* @param {any} content -
* Converts rich text to plain text
*
* @param content - The rich text content
*/
export function richToPlain(content) {
return this.formatLinksPlain(content)
export function richToPlain(content: string) {
return formatLinksPlain(content)
}

/**
* @param {any} content -
* Format links in the given content to rich text links
*
* @param content - The content containing plain text URLs
*/
export function formatLinksRich(content) {
export function formatLinksRich(content: string) {
return content.replace(urlRegex, function(_, leadingSpace, protocol, url, trailingSpace) {
let linkText = url
if (!protocol) {
Expand All @@ -49,13 +53,16 @@ export function formatLinksRich(content) {
}

/**
* @param {any} content -
* Format links in the given content to plain text links
*
* @param content - The content containing rich text URLs
*/
export function formatLinksPlain(content) {
const $content = $('<div></div>').html(content)
$content.find('a').each(function() {
const $this = $(this)
$this.html($this.attr('href'))
export function formatLinksPlain(content: string) {
const el = document.createElement('div')
el.innerHTML = content
el.querySelectorAll('a').forEach((anchor) => {
anchor.replaceWith(document.createTextNode(anchor.getAttribute('href') || ''))
})
return $content.html()

return el.innerHTML
}
5 changes: 4 additions & 1 deletion core/src/OCP/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { loadState } from '@nextcloud/initial-state'
import Accessibility from './accessibility.js'
import * as AppConfig from './appconfig.ts'
import Collaboration from './collaboration.js'
import * as Comments from './comments.js'
import * as Comments from './comments.ts'
import Loader from './loader.js'
import Toast from './toast.js'
import * as WhatsNew from './whatsnew.js'
Expand All @@ -17,6 +17,9 @@ export default {
Accessibility,
AppConfig,
Collaboration,
/**
* @deprecated 33.0.0
*/
Comments,
InitialState: {
/**
Expand Down
4 changes: 2 additions & 2 deletions dist/core-main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/core-main.js.map

Large diffs are not rendered by default.

Loading