Skip to content

Commit

Permalink
Merge pull request #3 from namecheap/feat/marked_parts_to_remove
Browse files Browse the repository at this point in the history
feat: add tags for removing parts from template
  • Loading branch information
m2broth authored Oct 7, 2024
2 parents fc5bd3d + 137d68a commit 8ce99f3
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/parse-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ module.exports = (handledTags, baseTemplatesCacheSize) => {
Promise.resolve().then(() => {
const templateCutter = new TemplateCutter();

const cuttedBaseTemplate = templateCutter.cut(baseTemplate);
const cuttedBaseTemplate = templateCutter.cut(
templateCutter.remove(baseTemplate)
);

const chunks = transform.applyTransforms(
cuttedBaseTemplate,
Expand Down
16 changes: 16 additions & 0 deletions lib/template-cutter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const MARKED_PARTS_TO_IGNORE =
/<!-- TailorX: Ignore during parsing START -->.*?<!-- TailorX: Ignore during parsing END -->/gims;
const IGNORED_PART_WITH_INDEX =
/<!-- TailorX: Ignored content during parsing #(\d+) -->/gm;
const MARKED_PARTS_TO_REMOVE =
/<!-- TailorX: Remove before parsing START -->.*?<!-- TailorX: Remove before parsing END -->/gims;

function replaceIgnorePart(ignoredPartIndex) {
return `<!-- TailorX: Ignored content during parsing #${ignoredPartIndex} -->`;
Expand Down Expand Up @@ -31,6 +33,20 @@ class TemplateCutter {
});
}

/**
* Remove base templates special parts which marked with the help of special HTML comments:
* <!-- TailorX: Remove before parsing START --> and <!-- TailorX: Remove before parsing END -->
* before and after a content which you want to remove before parsing
*
* @example <!-- TailorX: Remove before parsing START --><div class='example-class>Example</div><!-- TailorX: Remove before parsing END -->
*
* @param {String} baseTemplate - Base template that contains all the necessary tags and fragments for the given page (Used by multiple pages)
* @returns {String} Base template without removed parts
*/
remove(baseTemplate) {
return baseTemplate.replace(MARKED_PARTS_TO_REMOVE, '');
}

/**
* Restore base template's special parts after ignoring them by @method _cut
*
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@namecheap/tailorx",
"version": "8.1.0",
"version": "8.2.0",
"description": "Tailor assembles a web page from multiple fragments",
"keywords": [
"tailor",
Expand Down
38 changes: 38 additions & 0 deletions tests/template-cutter.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,42 @@ describe('TemplateCutter', () => {
'TailorX can not find an ignored part 100 of the current template during restoring!'
);
});

it('should remove marked parts from the base template', () => {
const baseTemplate =
'<!DOCTYPE html>' +
'<html>' +
'<head>' +
'<meta charset="utf-8" />' +
'<meta name="viewport" content="width=device-width,initial-scale=1" />' +
'</head>' +
'<body>' +
'<!-- TailorX: Remove before parsing START -->' +
'<div id="remove-this">' +
'<slot name="remove-this"></slot>' +
'</div>' +
'<!-- TailorX: Remove before parsing END -->' +
'<div id="keep-this">' +
'<slot name="keep-this"></slot>' +
'</div>' +
'</body>' +
'</html>';

const expectedTemplate =
'<!DOCTYPE html>' +
'<html>' +
'<head>' +
'<meta charset="utf-8" />' +
'<meta name="viewport" content="width=device-width,initial-scale=1" />' +
'</head>' +
'<body>' +
'<div id="keep-this">' +
'<slot name="keep-this"></slot>' +
'</div>' +
'</body>' +
'</html>';

const result = templateCutter.remove(baseTemplate);
assert.equal(result, expectedTemplate);
});
});

0 comments on commit 8ce99f3

Please sign in to comment.