Skip to content

Remove dependency on EJS#990

Merged
yamgent merged 5 commits into
MarkBind:masterfrom
le0tan:remove-ejs
Jan 30, 2020
Merged

Remove dependency on EJS#990
yamgent merged 5 commits into
MarkBind:masterfrom
le0tan:remove-ejs

Conversation

@le0tan

@le0tan le0tan commented Jan 20, 2020

Copy link
Copy Markdown
Contributor

What is the purpose of this pull request? (put "X" next to an item, remove the rest)

• [X] Other, please explain:

Remove unnecessary dependency of ejs.

Resolves #988

What is the rationale for this request?

It seems that MarkBind is using both nunjucks and ejs as templating engines. However ejs is only used to render page.ejs. It's probably a bit unnecessary to keep ejs to use it only once while it can be replaced with nunjucks?

What changes did you make? (Give an overview)

Replace page.ejs with page.njk and change the initial template rendering from EJS to Nunjucks.

Provide some example code that this change will affect:

Ideally this shouldn't change any behavior.

Is there anything you'd like reviewers to focus on?

Is variables still working as expected? As I changed one test on variables referring to another variable.

Testing instructions:

npm run testwin should do.

Proposed commit message: (wrap lines at 72 characters)

Remove dependency on EJS

MarkBind is using two templating engines (i.e. EJS and Nunjucks) while
Nunjucks is essential to the functionality since it's user accessible,
EJS seems a bit redundant and can be fully replaced by Nunjucks.

  • Re-write page.ejs with Nunjucks grammar
  • Make necessary changes to remove dependency on EJS
  • Change test on nested variable reference as initial Nunjucks renderer
    is set to not auto-escape

@le0tan

le0tan commented Jan 20, 2020

Copy link
Copy Markdown
Contributor Author

This is the failed test case:

Site resolves variables referencing other variables

    expect(received).toEqual(expected) // deep equality

    Expected: "<span style="color: blue">Blue text</span>"
    Received: "<span style=\"color: blue\">Blue text</span>"

      367 |     .replace(/>/g, '&gt;');
      368 |   expect(root.level3).toEqual(expectedTextSpan);
    > 369 |   expect(root.level4).toEqual(expectedTextSpanEscaped);
          |                       ^
      370 | });
      371 | 
      372 | test('Site read correct user defined variables', async () => {

      at Object.<anonymous> (test/unit/Site.test.js:369:23)

@openorclose do you have any ideas?

@openorclose

Copy link
Copy Markdown
Contributor

This is the failed test case:

Site resolves variables referencing other variables

    expect(received).toEqual(expected) // deep equality

    Expected: "&lt;span style=&quot;color: blue&quot;&gt;Blue text&lt;/span&gt;"
    Received: "<span style=\"color: blue\">Blue text</span>"

      367 |     .replace(/>/g, '&gt;');
      368 |   expect(root.level3).toEqual(expectedTextSpan);
    > 369 |   expect(root.level4).toEqual(expectedTextSpanEscaped);
          |                       ^
      370 | });
      371 | 
      372 | test('Site read correct user defined variables', async () => {

      at Object.<anonymous> (test/unit/Site.test.js:369:23)

@openorclose do you have any ideas?

image

Not sure what the test is testing, but when i tried it on a real site it does seem to produce the non-escaped version...

@yamgent

yamgent commented Jan 21, 2020

Copy link
Copy Markdown
Member

From the issue tracker:

It seems that MarkBind is using both nunjucks and ejs as templating engines. However ejs is only used to render page.ejs. It's probably a bit unnecessary to keep ejs to use it only once while it can be replaced with nunjucks?

Yes, technical-wise, there shouldn't be any problem with replacing ejs with nunjucks. Iirc, the main argument back then for using ejs was that since this isn't exposed to the users at all, using ejs was a better choice because it was closer to javascript in terms of design, which is what we developers use.

However, given the current nature of the project + possibility of making maintainability better, I am open to switching out the ejs.

Not sure what the test is testing, but when i tried it on a real site it does seem to produce the non-escaped version...

If nunjucks is set to autoescape mode, the variable will came up empty, which is why the escaping was necessary back then. If this PR has changed the behaviour, such that the render will still be correct even without escaping, then the test should be changed to reflect that (i.e. expect(root.level4).toEqual(expectedTextSpan);).

@le0tan

le0tan commented Jan 21, 2020

Copy link
Copy Markdown
Contributor Author

Not sure what the test is testing, but when i tried it on a real site it does seem to produce the non-escaped version...

From the description of this test, I suppose my PR is working correctly?

@openorclose

Copy link
Copy Markdown
Contributor

Not sure what the test is testing, but when i tried it on a real site it does seem to produce the non-escaped version...

From the description of this test, I suppose my PR is working correctly?

Yeah, from what @yamgent commented, since now your nunjucks autoescape is set to false you can just edit the tests to reflect it.

@le0tan le0tan marked this pull request as ready for review January 22, 2020 09:00
@le0tan le0tan changed the title [WIP] Remove ejs dependency Remove ejs dependency Jan 22, 2020
@le0tan le0tan requested a review from yamgent January 22, 2020 09:07
Comment thread src/Site.js
Comment thread src/page.njk Outdated
{% for link in asset.pluginLinks %}
{{ link }}
{% endfor %}
{% endif %}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There are now a lot of blank spaces in the output.

...
  <link rel="stylesheet" href="markbind/fontawesome/css/all.min.css">
  <link rel="stylesheet" href="markbind/glyphicons/css/bootstrap-glyphicons.min.css">
  <link rel="stylesheet" href="markbind/css/github.min.css">
  <link rel="stylesheet" href="markbind/css/markbind.css">



  <link rel="stylesheet" href="markbind/layouts/default/styles.css">

  <link rel="stylesheet" href="markbind/css/site-nav.css">


  <link rel="stylesheet" href="markbind/css/page-nav.css">



</head>

You can get rid of the blank spaces in the render by using whitespace control. For example, changing the above to this will (1) remove unnecessary whitespace, and (2) make the align with each other:

{%- if asset.pluginLinks %}
{%- for link in asset.pluginLinks %}
    {{ link }}
{%- endfor %}
{%- endif %}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The format and indentation in the original file src/page.ejs are like that due to the whitespace issue, so if unsure following the original one is the easiest.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I applied the whitespace control to the entire file. Thanks for the tips!

@le0tan le0tan requested a review from yamgent January 28, 2020 03:21
@yamgent yamgent added this to the v2.10.0 milestone Jan 30, 2020
@yamgent yamgent changed the title Remove ejs dependency Remove dependency on EJS Jan 30, 2020
@yamgent yamgent merged commit 2b378db into MarkBind:master Jan 30, 2020
yamgent added a commit to yamgent/markbind that referenced this pull request Feb 3, 2020
PR MarkBind#990 removed (and unintentionally added) some blank lines. The
changes in the blank lines are not reflected in the tests' expected
files.

Our tests do not fail because it ignores whitespaces. Nevertheless, it
is still out of sync with the actual output, which means that anyone
running `npm run updatetest` or `npm run updatetestwin` will always see
these noisy changes.

Let's update the expected files to match the blank lines in the actual
output.
nbriannl pushed a commit to nbriannl/markbind that referenced this pull request Feb 9, 2020
MarkBind is using two templating engines (i.e. EJS and Nunjucks) while
Nunjucks is essential to the functionality since it's user accessible,
EJS seems a bit redundant and can be fully replaced by Nunjucks.

* Re-write page.ejs with Nunjucks grammar
* Make necessary changes to remove dependency on EJS
* Change test on nested variable reference as initial Nunjucks renderer
  is set to not auto-escape
nbriannl pushed a commit to nbriannl/markbind that referenced this pull request Feb 9, 2020
PR MarkBind#990 removed (and unintentionally added) some blank lines. The
changes in the blank lines are not reflected in the tests' expected
files.

Our tests do not fail because it ignores whitespaces. Nevertheless, it
is still out of sync with the actual output, which means that anyone
running `npm run updatetest` or `npm run updatetestwin` will always see
these noisy changes.

Let's update the expected files to match the blank lines in the actual
output.
nbriannl pushed a commit to nbriannl/markbind that referenced this pull request Feb 9, 2020
MarkBind is using two templating engines (i.e. EJS and Nunjucks) while
Nunjucks is essential to the functionality since it's user accessible,
EJS seems a bit redundant and can be fully replaced by Nunjucks.

* Re-write page.ejs with Nunjucks grammar
* Make necessary changes to remove dependency on EJS
* Change test on nested variable reference as initial Nunjucks renderer
  is set to not auto-escape
nbriannl pushed a commit to nbriannl/markbind that referenced this pull request Feb 9, 2020
PR MarkBind#990 removed (and unintentionally added) some blank lines. The
changes in the blank lines are not reflected in the tests' expected
files.

Our tests do not fail because it ignores whitespaces. Nevertheless, it
is still out of sync with the actual output, which means that anyone
running `npm run updatetest` or `npm run updatetestwin` will always see
these noisy changes.

Let's update the expected files to match the blank lines in the actual
output.
le0tan added a commit that referenced this pull request Mar 29, 2020
#990 removes ejs as a dependency
@le0tan le0tan mentioned this pull request Mar 29, 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.

Remove ejs dependency for MarkBind

3 participants