-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
194 lines (158 loc) · 5.08 KB
/
index.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"use strict";
const through = require("through2");
const PluginError = require("plugin-error");
const htmlParser = require("node-html-parser");
const PLUGIN_NAME = "gulp-html-anchor-rewriter";
class Plugin {
/**
* The plugin config object.
* @typedef {Object} PluginConfig
* @property {string | Array.<string>} keyword For searching the anchor element href attribute value. If the href contains the keyword, the anchor will be processed.
* @property {string} rel For adding rel attribute.
* @property {string} target For adding target attribute.
* @property {boolean} whiteList Indicates whether the white-list mode is enabled.
*/
/**
* @type {PluginConfig}
*/
#options;
/**
* Creates a new instance of the plugin.
* @param {PluginConfig} options The configuration object.
*/
constructor(options) {
this.#options = options;
}
/**
* @returns {string}
*/
get #keyword() {
return this.#options["keyword"];
}
/**
* @returns {string}
*/
get #rel() {
return this.#options["rel"];
}
/**
* @returns {string}
*/
get #target() {
return this.#options["target"];
}
/**
* @returns {boolean}
*/
get #whiteList() {
return this.#options["whiteList"];
}
/**
* Runs the plugin logic.
* @param {string} content The raw content of the file.
* @returns {string}
*/
run(content) {
let that = this;
let html = htmlParser.parse(content);
let anchors = html.querySelectorAll("a");
anchors.forEach(function (element) {
that.#processAnchor(element, that.#keyword, that.#rel, that.#target, that.#whiteList);
});
return html.toString();
}
/**
* Validates the configuration options.
* @returns {PluginError}
*/
validate() {
if (!this.#options) {
return new PluginError(PLUGIN_NAME, "The options are missing!");
}
if (this.#keyword) {
let isValid = Array.isArray(this.#keyword) || typeof this.#keyword === "string";
if (!isValid) {
return new PluginError(PLUGIN_NAME, "The keyword must be string or array of the strings!");
}
}
}
/**
* Processes the rules for an anchor element.
* @param {htmlParser.HTMLElement} anchorElement The anchor element.
* @param {string | Array.<string>} keyword The keyword.
* @param {string} rel The rel attribute.
* @param {string} target The target attribute.
* @param {boolean} whiteList Indicates whether the white-list mode is enabled.
*/
#processAnchor(anchorElement, keyword, rel, target, whiteList) {
if (!keyword) {
this.#setAttribute(anchorElement, rel, target);
return;
}
let keywords = keyword;
if (typeof keyword === "string") {
keywords = [keyword];
}
if (!this.#canExecute(anchorElement, keywords, whiteList)) {
return;
}
this.#setAttribute(anchorElement, rel, target);
}
/**
* Sets the anchor attributes.
* @param {htmlParser.HTMLElement} anchorElement The anchor element.
* @param {string} rel The rel attribute.
* @param {string} target The target attribute.
*/
#setAttribute(anchorElement, rel, target) {
if (rel && !anchorElement.getAttribute("rel")) {
anchorElement.setAttribute("rel", rel);
}
if (target && !anchorElement.getAttribute("target")) {
anchorElement.setAttribute("target", target);
}
}
/**
* Determines an anchor element will be re-write or not.
* @param {htmlParser.HTMLElement} anchorElement The anchor element.
* @param {string | Array.<string>} keywords The array of the keywords.
* @param {boolean} whiteList Indicates whether the white-list mode is enabled.
* @returns {boolean}
*/
#canExecute(anchorElement, keywords, whiteList) {
var href = anchorElement.getAttribute("href");
var result = keywords.some(function (k) {
return href.indexOf(k) >= 0;
});
if (!whiteList) {
return result;
}
return !result;
}
}
function pluginFunction(options) {
var plugin = new Plugin(options);
return through.obj(function (file, _encoding, cb) {
let validationResult = plugin.validate();
if (validationResult) {
cb(validationResult, file);
return;
}
// ignore empty files.
if (file.isNull()) {
cb();
return;
}
// streaming is not supported.
if (file.isStream()) {
cb(new PluginError(PLUGIN_NAME, "The streaming is not supported!"), file);
return;
}
let content = file.contents.toString();
let pluginOutput = plugin.run(content);
file.contents = Buffer.from(pluginOutput);
cb(null, file);
});
}
// Exporting the plugin main function
module.exports = pluginFunction;