joplin / joplin/plugin-templates
preProcessTemplateBody regex: \S in a template literal collapses to a literal S
- Dominant language
- TypeScript
- Stars
- 207
- Forks
- 40
- Avg merge
- 7d 16h
- Merged PRs (30d)
- 2
Description
`preProcessTemplateBody` builds its match pattern inside a template literal:
```ts
const pattern = new RegExp(`^[^\S\n]*${prop}[^\S\n]*:.*`, "gm");
```
In a template literal `\S` is not a regex escape, it collapses to a literal `S` before `RegExp` ever sees it. The compiled pattern is:
```
^[^S\n]*template_title[^S\n]*:.*
```
So the intended "any whitespace except a newline" became "any character except `S` and a newline".
It mostly still works by accident, because leading whitespace does match `[^S\n]`. But the class now matches almost anything, so a line that merely ends with the property name is treated as a match:
```yaml
my_template_title: something
```
That line is matched as though it were `template_title`, and `wrapInQuotes` will rewrite it.
The fix is to escape the backslash, `[^\\S\n]`, so the regex receives `\S` as intended.
Found while looking at #156, but it is an independent defect and worth fixing on its own.
Contributor guide
No contributing guide indexed for this repository
Research direction
Find the preProcessTemplateBody entry point and inspect the pattern construction shown in the issue. Preserve the intended whitespace matching by ensuring the regular expression receives the escaped \S sequence, then verify that a line such as my_template_title: something is not incorrectly rewritten by wrapInQuotes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- content
- Issue type
- Bug
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 78/100