Skip to content

Syntax Error encountered in render-inline.js when rendering markdown file #548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
dustywill opened this issue Apr 7, 2025 · 5 comments
Labels
nuemark Related to nuemark package

Comments

@dustywill
Copy link

dustywill commented Apr 7, 2025

Describe the Bug

When running my application based on the blog demo. I get a syntax error in the render-inline.js file. It appears when a regular expression is encountered in my Markdown file. It seems to occur when the render encounters a regular expression in a code fence.

Environment

  • OS: Windows 11
  • Nue 1.0.0-RC.3 • Bun 1.2.8

Minimal Reproduction

The syntax error appears to choke when render-inline.js encounters the regular expression in the markdown. My markdown file includes this section

   ```markdown
   /^(?P<BSN>B[0-9A-Z]{4})$/
   ```

I was able to eliminate the syntax error by putting single backticks around the regular expression inside the code fence like this

```markdown
`/^(?P<BSN>B[0-9A-Z]{4})$/`
```

Logs & Additional Context

I have edited my render-inline to log each of the parameters as they are passed around the file to aid in troubleshooting. Here is the result of that output.

Logged in the console from render-inline

renderTokens - Log tokens: [{"text":"/^(?P<BSN>B"},{"is_tag":true,"name":"0-9A-Z","attr":{"class":""},"data":{}},{"is_var":true,"name":"4"},{"text":")$/"}] and opts: undefined
renderToken - Log token: {"text":"/^(?P<BSN>B"} and opts: {}
renderToken - Log token: {"is_tag":true,"name":"0-9A-Z","attr":{"class":""},"data":{}} and opts: {}
renderToken - Log token: {"is_var":true,"name":"4"} and opts: {}
rendderVariable - Log expression: 4 and data: {}

52 | export function renderInline(str, opts) {
53 | return str ? renderTokens(parseInline(str), opts) : ''
54 | }
55 |
56 | function renderVariable(expr, data) {
57 | const fn = new Function('data', return data.${expr})
^
SyntaxError: Unexpected number '.4'. Expected a ';' following a return statement.
at (C:\Users\byron\node_modules\nuemark\src\render-inline.js:57:18)

@nobkd nobkd added the nuemark Related to nuemark package label Apr 7, 2025
@nobkd
Copy link
Collaborator

nobkd commented Apr 7, 2025

In a code block I don't experience this issue. (and in a code block / inline code, the logged parsing should never happen).
Do you maybe have {4} not in a code block / inline code / escaped (like \{4}) somewhere? Or a wrongly indented code block, that cancels the beinning of another one?
If so, then nuemark seems to try replacing the content with a variable from front matter / data, but this fails with a number on variable start.

(I can try again on windows, but this is my current findings.)

@dustywill
Copy link
Author

dustywill commented Apr 8, 2025

I can reproduce this at will now that I have done all the testing I did yesterday. It is from this specific regular expression as it appears here The next line should be 3 Backticks, but I can't figure out how to make Github not take that as an end codeblock. If I add single backticks around the RegEx on the same line then it goes away.


### Regex Pattern for BSN Matching

```markdown
/^(?P<BSN>B[0-9A-Z]{4})$


This regex matches BSN (Basic String Number) codes, ensuring they start with 'B' followed by 4 alphanumeric characters.

I also just discovered, if I indent the Regex in the Codeblock it does not throw the error.

@nobkd
Copy link
Collaborator

nobkd commented Apr 8, 2025

Does the following error on your end (because it doesn't for me on both linux and win11)?

nue create nobkd/nue-var-in-codeblock-repro
nue -r nobkd-nue-var-in-codeblock-repro

The above downloads the main branch from my github repo nobkd/nue-var-in-codeblock-repro
The second line runs the project (we don't run projects created from a gh repo automatically)


Have you already tried deleting the .dist directory? rm -r .dist
Can you send the file it errors on, so I can take a look?


Tip

you can do the following in (commonmark) markdown, to include md codeblocks in codeblocks:

````markdown
```js
console.log('hello world')
```
````

basically start and end count of backticks have to be equal
(I think: end tag count can be longer or equal to start count. so longer backtick counts inside probably won't work properly)

@dustywill
Copy link
Author

dustywill commented Apr 8, 2025 via email

@nobkd
Copy link
Collaborator

nobkd commented Apr 8, 2025

That happens because nuemark tries to parse {...} in text as variable (see: #548 (comment), and docs), and the error occurs, because a JS variable can't start with a digit.

So replace {3} with \{3} in the raw text.
Also \ has to be escaped in raw text, if you want to have a backslash char (at least in nuemark atm) (therefore \\S)

  **For LabelMatch:**
- /^(?<BSN>\S{3})$/
+ /^(?<BSN>\\S\{3})$/

Hope that resolves it for you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
nuemark Related to nuemark package
Projects
None yet
Development

No branches or pull requests

2 participants