You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As currently written, this module performs interpolation before
stripping leading whitespace. In particular, whitespace is stripped from
the interpolated components. I think that this is the wrong behavior.
My understanding is that dedent is supposed to be effectively
syntactic sugar. Writing
functionfoo(){constthings=dedent`\ lorem ipsum dolor sit amet `;returnthings;}
should be completely equivalent to writing
functionfoo(){constthings=`\lorem ipsumdolor sit amet`;returnthings;}
with the sole benefit that the code indentation doesn’t have to become
wonky just to accommodate the text.
This suggests that
functionfoo(){conststuff="abc\n xyz";constthings=dedent`\ lorem ${stuff} dolor sit amet `;returnthings;}
should also be equivalent to
functionfoo(){conststuff="abc\n xyz";constthings=`\lorem ${stuff}dolor sit amet`;returnthings;}
which means that the output should be
lorem abc
xyz
dolor sit amet
but with the current version of dedent the result is actually
lorem abc
xyz
dolor sit amet
Let me provide a concrete case where this matters. I was using dedent
to server-side render static pages on my site. The code looked roughly
like this:
// lots of context, so this code is indented a bunch...constpage=dedent`\ <!DOCTYPE html> <html> <body> <div id="container">${html}</div> <script src="${pathToBundle}"></script> </body> </html> `;
This all worked well—I wrote it and forgot about it. But when I added
code blocks to my site, I noticed that they rendered incorrectly: code
like
<pre><code>function foo(bar, baz) {
while (true) {
if (bar()) {
console.log("stuff");
if (baz()) {
console.log(
"even more stuff"
);
}
}
}
}</code></pre>
was being rendered as
<pre><code>function foo(bar, baz) {
while (true) {
if (bar()) {
console.log("stuff");
if (baz()) {
console.log(
"even more stuff"
);
}
}
}
}</code></pre>
in the static HTML. Because pre sets white-space: pre, the resulting
code is rendered incorrectly in the browser, too. This was somewhat
difficult to track down!
I ended up replacing this module with a hand-written version that fit my
purposes better. (There was an additional change that I needed to make
to properly render code blocks with line-trailing backslashes.) You can
see that change here:
As currently written, this module performs interpolation before
stripping leading whitespace. In particular, whitespace is stripped from
the interpolated components. I think that this is the wrong behavior.
My understanding is that
dedent
is supposed to be effectivelysyntactic sugar. Writing
should be completely equivalent to writing
with the sole benefit that the code indentation doesn’t have to become
wonky just to accommodate the text.
This suggests that
should also be equivalent to
which means that the output should be
but with the current version of
dedent
the result is actuallyLet me provide a concrete case where this matters. I was using
dedent
to server-side render static pages on my site. The code looked roughly
like this:
This all worked well—I wrote it and forgot about it. But when I added
code blocks to my site, I noticed that they rendered incorrectly: code
like
was being rendered as
in the static HTML. Because
pre
setswhite-space: pre
, the resultingcode is rendered incorrectly in the browser, too. This was somewhat
difficult to track down!
I ended up replacing this module with a hand-written version that fit my
purposes better. (There was an additional change that I needed to make
to properly render code blocks with line-trailing backslashes.) You can
see that change here:
wchargin/wchargin.github.io@06475d4
Is this something that you’re interested in pulling upstream? I realize
that it is a breaking change, so I’d understand if you’re not
interested.
(Also, 👋 !)
The text was updated successfully, but these errors were encountered: