') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); })(); Remove dependency on EJS by le0tan · Pull Request #990 · MarkBind/markbind · GitHub
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

@le0tanle0tan 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

Copy link
Copy Markdown
ContributorAuthor

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

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

Copy link
Copy Markdown
ContributorAuthor

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
@le0tanle0tan changed the title [WIP] Remove ejs dependencyRemove ejs dependencyJan 22, 2020
@le0tan
le0tan requested a review from yamgentJanuary 22, 2020 09:07
Comment threadsrc/Site.js
Comment threadsrc/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.

...
<linkrel="stylesheet" href="markbind/fontawesome/css/all.min.css"><linkrel="stylesheet" href="markbind/glyphicons/css/bootstrap-glyphicons.min.css"><linkrel="stylesheet" href="markbind/css/github.min.css"><linkrel="stylesheet" href="markbind/css/markbind.css"><linkrel="stylesheet" href="markbind/layouts/default/styles.css"><linkrel="stylesheet" href="markbind/css/site-nav.css"><linkrel="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
ContributorAuthor

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 yamgentJanuary 28, 2020 03:21
@yamgentyamgent added this to the v2.10.0 milestone Jan 30, 2020
@yamgentyamgent changed the title Remove ejs dependencyRemove dependency on EJSJan 30, 2020
@yamgent
yamgent merged commit 2b378db into MarkBind:masterJan 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
@le0tanle0tan mentioned this pull request Mar 29, 2020
Sign up for freeto 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

@le0tan@openorclose@yamgent