html-table-patcher3.0.5
§ Quick Take
import { strict as assert } from "assert";
import { patcher } from "dist/html-table-patcher.esm";
assert.equal(
patcher(
`<table>
{% if customer.details.hasAccount %}
<tr>
<td>
variation #1
</td>
</tr>
{% else %}
<tr>
<td>
variation #2
</td>
</tr>
{% endif %}
</table>`
).result,
`<table>
<tr>
<td>
{% if customer.details.hasAccount %}
</td>
</tr>
<tr>
<td>
variation #1
</td>
</tr>
<tr>
<td>
{% else %}
</td>
</tr>
<tr>
<td>
variation #2
</td>
</tr>
<tr>
<td>
{% endif %}
</td>
</tr>
</table>`
);
§ Idea
When we make email templates, sooner or later, we have to add conditional logic around table tags. If this, show that row, if that, show another column, and so on. As a result, when we try to preview HTML in the browser, all the logic is placed at the wrong place, at the top, making it hard to visually QA.
This program patches table structures, creating cells as needed and placing the code in those cells so that when you preview, templating literals are in the correct places.
It is another helper tool to QA email campaigns.
§ API
This package exports a plain object with three keys: { patcher, defaults, version }
.
The first-one has a value which is the main function. The second one is the defaults (plain) object. The third one is the version taken straight from package.json
§ patcher()
API - Input
{ patcher, defaults, version }(str, [opts])
Input argument | Key value's type | Obligatory? | Description |
---|---|---|---|
str | String | yes | The input string of zero or more characters |
opts | Plain object | no | An Optional Options Object. See below for its API. |
§ patcher()
API - Output
It returns a plain object:
Key's name | Key value's type | Description |
---|---|---|
result | String | Result string containing patched code |
For example,
{
result: `<table>
<tr>
<td>
foo
</td>
</tr>
<tr>
<td>
bar
</td>
</tr>
</table>`
}
§ patcher()
API - Options
Put options under function's second input argument, in a plain object, as per defaults:
import { patcher, defaults, version } from "html-table-patcher";
const result = patcher("<table>1<tr><td>zzz</td></tr></table>", {
// <---- options object
cssStylesContent: "",
alwaysCenter: false,
});
Here's the options object's API:
Options Object's key | Value's type | Default value | Description |
---|---|---|---|
cssStylesContent | string | "" (empty string) | Whatever you put here, will end up on every newly-added TD's inline style tag's value |
alwaysCenter | boolean | false | Every newly-added TD should have its contents centered (via an inline align="center" HTML attribute) |
§ The algorithm
We parse using codsen-parser
then traverse using ast-monkey-traverse-with-lookahead
and record what needs to be amended using ranges-push
and finally, apply all changes in one go using ranges-apply
.
§ Using the GUI tap
Git clone the repo to your SSD.
Then, cd
into the root of this package (where its pacakge.json
is).
Then, serve
the folder to localhost server.
If you open the folder /tap/
in browser, you'll get mini GUI application which is driven by Vue (local copy, no Internet needed) and also by local code of the pacher.
Don't serve
from inside /tap/
folder because virtual server won't "see" the source code at the level above its root, ../dist/*
. serve
from the root.
§ Changelog
See it in the monorepo , on Sourcehut.
§ Licence
Copyright © 2010–2020 Roy Revelt and other contributors