-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
87 lines (70 loc) · 3.06 KB
/
main.js
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
class KeyReplacementPlugin {
constructor(API, name, config) {
this.API = API;
this.name = name;
this.config = config;
// Ensure 'rules' is an array
if (!Array.isArray(this.config.rules)) {
this.config.rules = [];
this.saveConfig();
}
}
// Register the 'htmlOutput' modifier
addModifiers() {
this.API.addModifier('htmlOutput', this.replaceKeysInContent, 1, this);
}
/**
* Replace keys in the HTML content with their corresponding replacements.
* @param {string} htmlCode - The HTML content of the page/post.
* @param {object} globalContext - Additional context if needed.
* @param {object} context - Specific context of the rendering process.
* @returns {string} - Modified HTML content.
*/
replaceKeysInContent(renderer, htmlCode, globalContext, context) {
// Ensure htmlCode is a string
if (typeof htmlCode !== 'string') {
console.error('htmlCode is not a string:', htmlCode);
return htmlCode; // Return original content if not a string
}
console.log('==================================================================')
let modifiedHtml = htmlCode;
// Iterate over each replacement rule
this.config.rules.forEach(rule => {
if (rule.key && rule.replacement) {
// Convert replacement to string to avoid type issues
const replacement = String(rule.replacement);
// Escape regex special characters in key
const escapedKey = this.escapeRegExp(rule.key);
// Log the replacement for debugging
console.log(`Replacing [${rule.key}] with ${replacement}`);
// Regex to find <p>[Key]</p> possibly with spaces
const regexWithPTags = new RegExp(`<p>\\s*${escapedKey}\\s*<\\/p>`, 'g');
// Replace <p>[Key]</p> with replacement
modifiedHtml = modifiedHtml.replace(regexWithPTags, replacement);
// Regex to find [Key] not within <p> tags
const regexKey = new RegExp(`${escapedKey}`, 'g');
// Replace [Key] with replacement
modifiedHtml = modifiedHtml.replace(regexKey, replacement);
} else {
console.warn('Invalid rule detected:', rule);
}
});
// Log the modified HTML for debugging
console.log('Modified HTML:', modifiedHtml);
return modifiedHtml;
}
/**
* Utility function to escape special characters in a string for use in a RegExp.
* @param {string} string - The string to escape.
* @returns {string} - Escaped string.
*/
escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
// Save updated configuration
saveConfig() {
this.API.saveConfig(this.config);
}
}
// Export the plugin class
module.exports = KeyReplacementPlugin;