-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
96 lines (86 loc) · 2.97 KB
/
parser.js
File metadata and controls
96 lines (86 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* Marketo Template Parser
*
* Speeds up the development of Marketo templates by prefilling the default values for the mkto meta tags.
*
* @link https://github.com/stuartajd/marketo-template-parser
* @author Stuart Davidson <me@stuartd.co.uk>
* @version 0.4
*/
(() => {
/**
* Configuration options for the specific tags that may change value
*/
const mktoFunctionMap = {
mktoBoolean: mktoBoolean
}
/**
* Sets the value of any elements that need have a boolean meta tag.
*/
function mktoBoolean(htmlContent, tag) {
const value = tag.default === 'true' ? tag.true_value : tag.false_value;
return htmlContent.replaceAll("${"+ tag.id +"}", value);
}
/**
* Sets the value of any elements to be the default value.
*/
function mktoDefault(htmlContent, tag) {
const value = tag.allowhtml === 'true' ? tag.default : escapeHTML(tag.default);
return htmlContent.replaceAll("${"+ tag.id +"}", `${value}${(tag.units ?? '')}`);
}
/**
* Escapes HTML special characters in a string.
*/
function escapeHTML(string) {
return string.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
/**
* Parses the document and updates Marketo tags with default values.
*/
function parseDocument() {
console.groupCollapsed('[Marketo Template Parser script]');
console.info("Starting to parse document...");
/**
* Check if there is atleast one mktoMeta Element, otherwise stop.
*/
const hasOneMeta = document.documentElement.querySelector("meta[class^=mkto]");
if(!hasOneMeta) {
console.info("Could not find any Marketo attributes");
return console.groupEnd();
}
let htmlContent = document.documentElement.innerHTML;
/**
* Grab out the meta elements that contain Marketo editable tags.
* Update each of the elements & update our htmlContent variable.
* Set the innerHTML of the document to be our htmlContent once for performance.
*/
const marketoAttributes = [...document.documentElement.querySelectorAll("meta[class^=mkto]")].map((tag) => {
return {
id: tag.id,
type: tag.classList[0],
name: tag.getAttribute("mktoName"),
default: tag.getAttribute("default"),
units: tag.getAttribute("units"),
allowhtml: tag.getAttribute("allowhtml"),
true_value: tag.getAttribute("true_value"),
false_value: tag.getAttribute("false_value"),
true_value_name: tag.getAttribute("true_value_name"),
false_value_name: tag.getAttribute("false_value_name")
}
});
marketoAttributes.forEach((tag) => {
const mktoFunction = mktoFunctionMap[tag.type] || mktoDefault;
htmlContent = mktoFunction(htmlContent, tag);
});
console.log(`Parsing completed, ${marketoAttributes.length} tags found`);
console.log('Marketo attributes found:', marketoAttributes);
document.documentElement.innerHTML = htmlContent;
console.info("Document content updated");
console.groupEnd();
}
window.addEventListener('DOMContentLoaded', parseDocument);
})();