Skip to content

Commit

Permalink
Add new test and new function before DW look at hook:
Browse files Browse the repository at this point in the history
  • Loading branch information
jgclark committed Jun 13, 2023
1 parent e03313f commit 6ac2e9f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 17 deletions.
15 changes: 15 additions & 0 deletions helpers/__tests__/urls.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,21 @@ describe(`${PLUGIN_NAME}`, () => {
])
})

it('should find hook mark links', () => {
const text = 'Hello Example hook://file/something to somewhere'
const result = f.findURLsInText(text, false)
expect(result).toEqual([
{
url: 'hook://file/something',
name: 'Example',
lineIndex: 0,
domain: '',
page: '',
type: 'bareURL',
},
])
})

it('should handle text without URLs', () => {
const text = 'Hello World'
const result = f.findURLsInText(text)
Expand Down
75 changes: 58 additions & 17 deletions helpers/urls.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/* @flow */

import { logError } from '@helpers/dev'
import { findEndOfActivePartOfNote } from "@helpers/paragraph"
import { isClosed } from '@helpers/utils'

export type LinkObject = {
url: string,
type: 'markdown' | 'bareURL',
Expand Down Expand Up @@ -42,28 +46,65 @@ export function processURL(urlStr: string, name: ?string, lineIndex: number, rem
* @param {boolean} [removeSubdomain=false] - Whether to remove the subdomain (like www) from the URLs or not.
* @returns {LinkObject[]} An array of LinkObjects.
*/
export function findURLsInText(text: string, removeSubdomain: boolean = false): Array<LinkObject> {
const markdownURLPattern = /\[([^\]]+)\]\(([^)]+)\)/g // Match markdown URLs with or without 'http(s)://'
const bareURLPattern = /(https?:\/\/[^\s]+)/g
export function findURLsInText(
text: string,
removeSubdomain: boolean = false,
): Array<LinkObject> {
try {
const markdownURLPattern = /\[([^\]]+)\]\(([^)]+)\)/g // Match markdown URLs with or without 'http(s)://'
const bareURLPattern = /(https?:\/\/[^\s]+)/g

const lines = text.split('\n')
const links: Array<LinkObject> = []
const lines = text.split('\n')
const links: Array<LinkObject> = []

for (let i = 0; i < lines.length; i++) {
let line = lines[i]
let match
for (let i = 0; i < lines.length; i++) {
let line = lines[i]
let match

// Process markdown URLs first and replace them with placeholders in the line.
while ((match = markdownURLPattern.exec(line)) !== null) {
links.push(processURL(match[2], match[1], i, removeSubdomain))
line = line.replace(match[0], 'MARKDOWN_LINK_PLACEHOLDER')
}
// Process markdown URLs first and replace them with placeholders in the line.
while ((match = markdownURLPattern.exec(line)) !== null) {
links.push(processURL(match[2], match[1], i, removeSubdomain))
line = line.replace(match[0], 'MARKDOWN_LINK_PLACEHOLDER')
}

// Process bare URLs.
while ((match = bareURLPattern.exec(line)) !== null) {
links.push(processURL(match[1], null, i, removeSubdomain))
// Process bare URLs.
while ((match = bareURLPattern.exec(line)) !== null) {
links.push(processURL(match[1], null, i, removeSubdomain))
}
}

return links
} catch (err) {
logError('findURLsInText', err.message)
return []
}
}

return links
/**
* Scans a note for URLs and returns an array of LinkObjects.
*
* @param {TNote} note - The note to scan for URLs.
* @param {boolean} [removeSubdomain=false] - Whether to remove the subdomain (like www) from the URLs or not.
* @param {boolean} [searchOnlyActivePart=true] - Whether to search the note's archive (Done and Cancelled sections) as well?
* @param {boolean} [ignoreCompletedItems=false] - Whether to ignore URLs in done/cancelled tasks/checklist items.
* @returns {LinkObject[]} array of LinkObjects
*/
export function findURLsInNote(
note: TNote,
removeSubdomain: boolean = false,
searchOnlyActivePart: boolean = true,
ignoreCompletedItems: boolean = false,
): Array<LinkObject> {
try {
const lastLineToSearch = searchOnlyActivePart ? findEndOfActivePartOfNote(note) : note.paragraphs.length - 1
let parasToSearch = note.paragraphs.filter(p => p.lineIndex <= lastLineToSearch)
if (ignoreCompletedItems) {
parasToSearch = parasToSearch.filter(p => !isClosed(p))
}
const textToSearch = parasToSearch.map(p => p.content).join('\n') ?? []
return findURLsInText(textToSearch, removeSubdomain)
} catch (err) {
logError('findURLsInNote', err.message)
return []
}
}

0 comments on commit 6ac2e9f

Please sign in to comment.