Skip to content
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

Markdown lexer, code_inline not closed for empty backtick pairs #115

Open
jxzwp opened this issue Jul 11, 2024 · 2 comments
Open

Markdown lexer, code_inline not closed for empty backtick pairs #115

jxzwp opened this issue Jul 11, 2024 · 2 comments

Comments

@jxzwp
Copy link

jxzwp commented Jul 11, 2024

Open textadept 12.4, on the menu go to Tools > Quick Open > Quickly Open Textadept Home.
Select docs/api.md from the filter list and click OK to open it.
Go to line 7533, which is under the heading for textadept.editing.auto_pairs.
On line 7533 there's a pair of empty backticks, after them the text is styled incorrectly, as though the rest of the text is inside a code block. It's easier to see if you're using a dark theme.

The problem is in the code_inline LPEG pattern in lexers/markdown.lua on line 36. Here

local code_inline = lpeg.Cmt(lpeg.C(P('`')^1), function(input, index, bt)

The pattern matches any number of leading backticks and then calls a function to determine where the inline code ends. However it interprets any even number sequence of empty backticks as an un-closed region of inline code. Adding an if statement to handle those edge cases fixes the issue. See the new line in the example fix below.

local code_inline = lpeg.Cmt(lpeg.C(P('`')^1), function(input, index, bt)
  -- `foo`, ``foo``, ``foo`bar``, `foo``bar` are all allowed.
  local _, e = input:find('[^`]' .. bt .. '%f[^`]', index)
  if not e and (#bt % 2 == 0) then return index end  --<<<<< New line <<<<<<
  return (e or #input) + 1
end)

When you test this, other interactions with the code_line and code_block LPEG patterns can make it difficult to determine which pattern is causing which styling, but the above fix does work as intended as far as I can tell.

Thanks for all the time you've spent developing Textadept.

@jxzwp jxzwp changed the title Markdown lexer, code_inline not closed for empy backtick pairs Markdown lexer, code_inline not closed for empty backtick pairs Jul 12, 2024
@orbitalquark
Copy link
Owner

orbitalquark commented Jul 16, 2024

Sorry for the delayed response.

Thanks so much for the detailed report and potential fix.

I'm torn on this one because a `` sequence is most likely the beginning of a code sequence. What you found is a product of an HTML generation bug (there should probably be an escape of some sort). However, as you point out, the unfortunate side effect is highlighting the rest of the document incorrectly, so a fix seems reasonable here.

I'll look into fixing the HTML generation bug first. Then I may circle back to this.

For what it's worth, the Markdown lexer is ripe for another refactor. I have a number of issues with it...

@jxzwp
Copy link
Author

jxzwp commented Jul 20, 2024

No worries on the delayed response. Thanks for acknowledging the issue.

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

No branches or pull requests

2 participants