Skip to content

Commit 91eb512

Browse files
committed
fix regex in paragraph
1 parent fd36be9 commit 91eb512

File tree

1 file changed

+40
-30
lines changed

1 file changed

+40
-30
lines changed

helpers/paragraph.js

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@ import { getDateStringFromCalendarFilename } from './dateTime'
77
import { clo, logDebug, logError, logInfo, logWarn } from './dev'
88
import { getElementsFromTask } from './sorting'
99
import { endOfFrontmatterLineIndex } from '@helpers/NPFrontMatter'
10-
import { RE_DONE_MENTION, RE_EVENT_LINK, RE_MARKDOWN_LINK_PATH_CAPTURE, RE_MARKDOWN_LINK_PATH_CAPTURE_G, RE_NOTELINK_G, RE_SIMPLE_URI_MATCH, RE_SIMPLE_URI_MATCH_G } from '@helpers/regex'
10+
import {
11+
RE_DONE_MENTION,
12+
RE_EVENT_LINK,
13+
RE_MARKDOWN_LINK_PATH_CAPTURE,
14+
RE_MARKDOWN_LINK_PATH_CAPTURE_G,
15+
RE_NOTELINK_G,
16+
RE_SIMPLE_URI_MATCH,
17+
RE_SIMPLE_URI_MATCH_G,
18+
} from '@helpers/regex'
1119
import { getLineMainContentPos } from '@helpers/search'
1220
import { stripLinksFromString } from '@helpers/stringTransforms'
1321

@@ -21,11 +29,10 @@ function caseInsensitiveSubstringMatch(searchTerm: string, textToSearch: string)
2129
try {
2230
// First need to escape any special characters in the search term
2331
const escapedSearchTerm = searchTerm.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
24-
const re = new RegExp(`${escapedSearchTerm}`, "i") // = case insensitive match
25-
logDebug('caseInsensitiveSubstringMatch', `re: ${String(re)} / textToSearch: ${textToSearch} / ${String(re.test(textToSearch))}`)
32+
const re = new RegExp(`${escapedSearchTerm}`, 'i') // = case insensitive match
33+
logDebug('caseInsensitiveSubstringMatch', `re: ${re.toString()} / textToSearch: ${textToSearch} / ${String(re.test(textToSearch))}`)
2634
return re.test(textToSearch)
27-
}
28-
catch (error) {
35+
} catch (error) {
2936
logError('paragraph/caseInsensitiveSubstringMatch', `Error matching '${searchTerm}' to '${textToSearch}': ${error.message}`)
3037
return false
3138
}
@@ -35,7 +42,7 @@ function caseInsensitiveSubstringMatch(searchTerm: string, textToSearch: string)
3542
* Return a version of 'input' that removes the path of any markdown links, and any URLs, and the contents of any note links.
3643
* Also trims off whitespace from the result.
3744
* @author @jgclark
38-
*
45+
*
3946
* @param {string} input
4047
* @returns {string}
4148
*/
@@ -59,7 +66,7 @@ export function stripAllURIsAndNoteLinks(input: string): string {
5966
/**
6067
* Remove (first) @done(YYYY-MM-DD HH:MM[AM|PM]) mention from a string
6168
* @author @jgclark
62-
*
69+
*
6370
* @tests available in jest file
6471
* @param {string} input
6572
* @returns {string}
@@ -90,7 +97,7 @@ export function isTermInURL(term: string, searchString: string): boolean {
9097

9198
/**
9299
* Is 'term' (typically a #tag) found in a string potentially containing a URL [[...]] or a URL in a string which may contain 0 or more notelinks and URLs?
93-
*
100+
*
94101
* @tests available in jest file
95102
* @param {string} term - term to check for
96103
* @param {string} input - string to search in
@@ -187,22 +194,22 @@ export function isTermInEventLinkHiddenPart(term: string, input: string): boolea
187194
*/
188195
export function isTermInMarkdownPath(term: string, searchString: string): boolean {
189196
try {
190-
// create version of searchString that doesn't include the URL and test that first
191-
const MDPathMatches = searchString.match(RE_MARKDOWN_LINK_PATH_CAPTURE) ?? []
192-
const thisMDPath = MDPathMatches[1] ?? ''
193-
if (thisMDPath !== '') {
194-
const restOfLine = searchString.replace(thisMDPath, '')
195-
// logDebug('isTermInMarkdownPath', `MDPathMatches: ${String(MDPathMatches)} / thisMDPath: ${thisMDPath} / restOfLine: ${restOfLine}`)
196-
if (caseInsensitiveSubstringMatch(term, restOfLine)) {
197-
// logDebug('isTermInMarkdownPath', `Found in rest of line -> false`)
198-
return false
197+
// create version of searchString that doesn't include the URL and test that first
198+
const MDPathMatches = searchString.match(RE_MARKDOWN_LINK_PATH_CAPTURE) ?? []
199+
const thisMDPath = MDPathMatches[1] ?? ''
200+
if (thisMDPath !== '') {
201+
const restOfLine = searchString.replace(thisMDPath, '')
202+
// logDebug('isTermInMarkdownPath', `MDPathMatches: ${String(MDPathMatches)} / thisMDPath: ${thisMDPath} / restOfLine: ${restOfLine}`)
203+
if (caseInsensitiveSubstringMatch(term, restOfLine)) {
204+
// logDebug('isTermInMarkdownPath', `Found in rest of line -> false`)
205+
return false
206+
} else {
207+
return caseInsensitiveSubstringMatch(term, thisMDPath)
208+
// earlier: create tailored Regex to test for presence of the term
209+
// const testTermInMDPath = `\[.+?\]\([^\\s]*?${term}[^\\s]*?\)`
210+
}
199211
} else {
200-
return caseInsensitiveSubstringMatch(term, thisMDPath)
201-
// earlier: create tailored Regex to test for presence of the term
202-
// const testTermInMDPath = `\[.+?\]\([^\\s]*?${term}[^\\s]*?\)`
203-
}
204-
} else {
205-
// logDebug('isTermInMarkdownPath', `No MD path -> false`)
212+
// logDebug('isTermInMarkdownPath', `No MD path -> false`)
206213
return false
207214
}
208215
} catch (error) {
@@ -341,7 +348,7 @@ export function smartPrependPara(note: TNote, paraText: string, paragraphType: P
341348
* Note: does work on a single line too
342349
* @author @jgclark
343350
* @test in jgclark.QuickCapture/index.js
344-
*
351+
*
345352
* @param {TNote} note - the note to append to
346353
* @param {Array<string>} paraTextArr - an array of text to append
347354
* @param {Array<ParagraphType>} paragraphTypeArr - a matching array of the type of the paragraphs to append
@@ -383,10 +390,10 @@ export function smartPrependParas(note: TNote, paraTextArr: Array<string>, paraT
383390
/**
384391
* Add a new paragraph and preceding heading(s) to a note. If the headings already exist, then don't add them again, but insert the paragraph after the existing headings.
385392
* @test in jgclark.QuickCapture/index.js
386-
*
393+
*
387394
* @param {TNote} destNote
388-
* @param {string} paraText
389-
* @param {ParagraphType} paragraphType
395+
* @param {string} paraText
396+
* @param {ParagraphType} paragraphType
390397
* @param {Array<string>} headingArray - the headings from H1 (or H2) downwards
391398
* @param {number} firstHeadingLevel - the level of the first heading given (1, 2, 3, etc.)
392399
* @param {boolean} shouldAppend - whether to append the paragraph after the headings or not.
@@ -397,7 +404,7 @@ export function smartCreateSectionsAndPara(
397404
paragraphType: ParagraphType,
398405
headingArray: Array<string>,
399406
firstHeadingLevel: number,
400-
shouldAppend: boolean = false
407+
shouldAppend: boolean = false,
401408
): void {
402409
try {
403410
// Work out which of the given headings already exist.
@@ -426,7 +433,10 @@ export function smartCreateSectionsAndPara(
426433
if (existingHeadingParas[i] !== '') {
427434
const thisHeadingPara = existingHeadingParas[i]
428435
latestInsertionLineIndex = thisHeadingPara.lineIndex + 1
429-
logDebug('paragraph/smartCreateSectionsAndPara', `noting existing heading "${thisHeadingPara.content}" at line ${String(latestInsertionLineIndex - 1)} level ${String(thisHeadingPara.headingLevel)}`)
436+
logDebug(
437+
'paragraph/smartCreateSectionsAndPara',
438+
`noting existing heading "${thisHeadingPara.content}" at line ${String(latestInsertionLineIndex - 1)} level ${String(thisHeadingPara.headingLevel)}`,
439+
)
430440
} else {
431441
// Heading doesn't exist, so add it
432442
let insertionIndex = 0
@@ -458,7 +468,7 @@ export function smartCreateSectionsAndPara(
458468
* Note: does work on a single line too.
459469
* @author @jgclark
460470
* @test in jgclark.QuickCapture/index.js
461-
*
471+
*
462472
* @param {TNote} note - the note to prepend to
463473
* @param {number} insertionIndex - the line to insert the text at
464474
* @param {Array<string>} paraTextArr - an array of text to prepend

0 commit comments

Comments
 (0)