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

Escape indented code block before render #4179

Conversation

seaoak
Copy link
Member

@seaoak seaoak commented Mar 11, 2020

What does it do?

Supplement of the PR #4171, for the issue #4087.

Since the PR #4171 changes rendering order,
Nunjucks tags in indented code blocks become targets of rendering by Nunjucks.
This is undesirable.

This situation can be observed in the issue #4087.

This patch escapes Nunjucks tags in indented code blocks before rendering by Nunjucks.

How to test

git clone -b feature/escape_indented_code_block_before_render https://github.com/seaoak/hexo.git
cd hexo
npm install
npm test

Pull request tasks

  • Add test cases for the changes.
  • Passed the CI test.

@seaoak
Copy link
Member Author

seaoak commented Mar 11, 2020

Code highlighting might be unnecessary for indented code blocks.

@SukkaW
Copy link
Member

SukkaW commented Mar 11, 2020

Code highlighting might be unnecessary for indented code blocks.

Yeah, there's no way for indented code blocks to add language mark.
Simply wrapped it inside <pre><code> </code></pre> will be ok then.

@seaoak seaoak force-pushed the feature/escape_indented_code_block_before_render branch from 54a68e4 to 843a12d Compare March 11, 2020 08:36
@SukkaW
Copy link
Member

SukkaW commented Mar 11, 2020

test test test
  hello world
test test
  hello
  world
test test
- a
  - b
    - c
      - d

What about adding test cases above to see if it can handle the situation.


Update

The current approach can not handle the second case above correctly.

@seaoak
Copy link
Member Author

seaoak commented Mar 12, 2020

@SukkaW I updated.
Your test cases are imported into test codes. Thank you!

@SukkaW
Copy link
Member

SukkaW commented Mar 12, 2020

Your test cases are imported into test codes. Thank you!

@seaoak I get a better idea.
Since it is hard for a regexp to do marked.js's job (to identify which line is nested list, which line is indented code block). What about not to wrapped code block inside <pre><code></code></pre> (this should be done by markdown renderer), only escape { & } for indented line then.

@stevenjoezhang
Copy link
Member

Users may use tags in nested list

- a
  - b
    - c
      - {% post_link 'hello world' %}

@seaoak seaoak force-pushed the feature/escape_indented_code_block_before_render branch from 78d65d1 to fbac10a Compare March 12, 2020 10:58
@seaoak
Copy link
Member Author

seaoak commented Mar 13, 2020

As @stevenjoezhang 's commnet, @SukkaW 's idea can not be used.
But I tried to make many test cases, the regexp will be OK.

@SukkaW
Copy link
Member

SukkaW commented Mar 13, 2020

But I tried to make many test cases, the regexp will be OK.

They might be many other cases that should be covered:

- a
  - b
    * c
      1. a
    * b
> 1. a
>   - b
>     1. c

And if the regexp is over complicated, the performance might be affected.

@seaoak
Copy link
Member Author

seaoak commented Mar 13, 2020

The condition to recognize "indented code block" is not so complicated.
It is whether the preceding line is a blank line or not.

Only in blockquote, the condition is a bit more complicated.

I think it is acceptable.

const rIndentedCodeBlock = /(^|(?:^|\n)(?:(?: {0,3}>){1,3}[^\S\r\n])?[^\S\r\n]*\n)((?:((?:(?: {0,3}>){1,3}[^\S\r\n])?)( {4}|\t)([^\n]*?)(\n|$))+)/g;

function indentedCodeBlock(data) {
data.content = data.content.replace(rIndentedCodeBlock, ($0, before, content, start, $4, $5, end) => {
Copy link
Member

@SukkaW SukkaW Mar 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
data.content = data.content.replace(rIndentedCodeBlock, ($0, before, content, start, $4, $5, end) => {
// Return early if there is no 4 consecutive spaces for better performance
if (!data.content.includes(' ')) return;
data.content = data.content.replace(rIndentedCodeBlock, ($0, before, content, start, $4, $5, end) => {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To support single-tab-indented code block, this optimization can not be used.

@seaoak
Copy link
Member Author

seaoak commented Mar 17, 2020

I Updated.

@seaoak
Copy link
Member Author

seaoak commented Mar 17, 2020

Unfortunately, I awake to the danger that this filter spoils indented codes in code block tag.

For example,

{% codeblock lang:javascript %}
if (a) {
  if (b) {
    if (c) {
      doSomething1();

      doSomething2();
    }
  }
}
{% endcodeblock %}

this filter escapes the line 7-8 with <pre><code>...</code></pre>.
This is undesirable.

I have no idea to resolve this. 😢

@seaoak
Copy link
Member Author

seaoak commented Mar 25, 2020

I'll discard this patch.

@stevenjoezhang stevenjoezhang linked an issue Apr 25, 2020 that may be closed by this pull request
5 tasks
@SukkaW SukkaW closed this Jul 19, 2020
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

Successfully merging this pull request may close these issues.

Markdown files are not rendered as expected
3 participants