@aiot-toolkit/shared-utils 1.0.17-beta.9 → 1.0.17-binary-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/buildMode/BuildModeManager.js +273 -1
- package/lib/buildMode/util.js +83 -1
- package/lib/index.js +488 -1
- package/lib/protobuf/protobuf.js +284 -0
- package/lib/protobuf/vdom_pb.js +2786 -0
- package/lib/record-client.js +150 -1
- package/package.json +3 -2
package/lib/index.js
CHANGED
|
@@ -1,2 +1,489 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.logWarn = logWarn;
|
|
7
|
+
exports.mkdirsSync = mkdirsSync;
|
|
8
|
+
exports.getIPv4IPAddress = getIPv4IPAddress;
|
|
9
|
+
exports.getServerIPAndPort = getServerIPAndPort;
|
|
10
|
+
exports.getDefaultServerHost = getDefaultServerHost;
|
|
11
|
+
exports.getClientIPAddress = getClientIPAddress;
|
|
12
|
+
exports.stripPrefixForIPV4MappedIPV6Address = stripPrefixForIPV4MappedIPV6Address;
|
|
13
|
+
exports.outputQRCodeOnTerminal = outputQRCodeOnTerminal;
|
|
14
|
+
exports.relateCwd = relateCwd;
|
|
15
|
+
exports.equals = equals;
|
|
16
|
+
exports.extend = extend;
|
|
17
|
+
exports.renderString = renderString;
|
|
18
|
+
exports.KnownError = KnownError;
|
|
19
|
+
exports.setCustomConfig = setCustomConfig;
|
|
20
|
+
exports.getProjectDslName = getProjectDslName;
|
|
21
|
+
exports.getDeviceInfo = getDeviceInfo;
|
|
22
|
+
exports.readJson = readJson;
|
|
23
|
+
exports.getManifest = getManifest;
|
|
24
|
+
exports.getUxExtensionList = getUxExtensionList;
|
|
25
|
+
exports.mergeJsonFile = mergeJsonFile;
|
|
26
|
+
exports.filterContentByTag = filterContentByTag;
|
|
27
|
+
Object.defineProperty(exports, "getLaunchPage", {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
get: function () {
|
|
30
|
+
return _util.getLaunchPage;
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
exports.colorconsole = exports.logger = void 0;
|
|
34
|
+
var _os = _interopRequireDefault(require("os"));
|
|
35
|
+
var _fs = _interopRequireDefault(require("fs"));
|
|
36
|
+
var _path = _interopRequireDefault(require("path"));
|
|
37
|
+
var _http = _interopRequireDefault(require("http"));
|
|
38
|
+
var _console = require("console");
|
|
39
|
+
var _chalk = _interopRequireDefault(require("chalk"));
|
|
40
|
+
var _qrcodeTerminal = _interopRequireDefault(require("qrcode-terminal"));
|
|
41
|
+
var _config = _interopRequireDefault(require("../config"));
|
|
42
|
+
var _parse = _interopRequireDefault(require("parse5"));
|
|
43
|
+
var _index = _interopRequireDefault(require("parse5/lib/serializer/index.js"));
|
|
44
|
+
var _util = require("./buildMode/util");
|
|
45
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
46
|
+
/**
|
|
47
|
+
* Copyright (C) 2017, hapjs.org. All rights reserved.
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
const logLevelMap = {};
|
|
51
|
+
function prependLevel(levelName, args) {
|
|
52
|
+
if (!logLevelMap[levelName]) {
|
|
53
|
+
const logLevel = levelName.toUpperCase();
|
|
54
|
+
logLevelMap[levelName] = logLevel;
|
|
55
|
+
}
|
|
56
|
+
if (typeof args[0] === 'string' && args[0].length > 1 && args[0][0] !== '[') {
|
|
57
|
+
args[0] = `[${logLevelMap[levelName]}] ${args[0]}`;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const logger = {
|
|
61
|
+
logs: [],
|
|
62
|
+
add(args) {
|
|
63
|
+
this.logs.push(args);
|
|
64
|
+
},
|
|
65
|
+
clear() {
|
|
66
|
+
this.logs = [];
|
|
67
|
+
},
|
|
68
|
+
get() {
|
|
69
|
+
return this.logs.join('\n');
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 带颜色的 info, log, warn, error, trace 的打印日志输出流
|
|
75
|
+
*/
|
|
76
|
+
exports.logger = logger;
|
|
77
|
+
let originConsole = global.console;
|
|
78
|
+
let console = _console.Console ? new _console.Console(process.stdout, process.stderr) : originConsole;
|
|
79
|
+
const colorconsole = {
|
|
80
|
+
attach(writable) {
|
|
81
|
+
if (writable && _console.Console) {
|
|
82
|
+
console = new _console.Console(writable, writable);
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
trace(...args) {
|
|
86
|
+
prependLevel(`trace`, args);
|
|
87
|
+
console.trace(...args);
|
|
88
|
+
},
|
|
89
|
+
log(...args) {
|
|
90
|
+
prependLevel(`log`, args);
|
|
91
|
+
console.log(_chalk.default.green(...args));
|
|
92
|
+
},
|
|
93
|
+
info(...args) {
|
|
94
|
+
prependLevel(`info`, args);
|
|
95
|
+
console.info(_chalk.default.green(...args));
|
|
96
|
+
},
|
|
97
|
+
warn(...args) {
|
|
98
|
+
prependLevel(`warn`, args);
|
|
99
|
+
console.warn(_chalk.default.yellow.bold(...args));
|
|
100
|
+
},
|
|
101
|
+
error(...args) {
|
|
102
|
+
prependLevel(`error`, args);
|
|
103
|
+
console.error(_chalk.default.red.bold(...args));
|
|
104
|
+
},
|
|
105
|
+
throw(...args) {
|
|
106
|
+
throw new Error(_chalk.default.red.bold(...args));
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* 打印日志
|
|
112
|
+
* @param loader
|
|
113
|
+
* @param logs
|
|
114
|
+
* @param suppresslog
|
|
115
|
+
*/
|
|
116
|
+
exports.colorconsole = colorconsole;
|
|
117
|
+
function logWarn(loader, logs, suppresslog) {
|
|
118
|
+
if (logs && logs.length) {
|
|
119
|
+
logs.forEach(log => {
|
|
120
|
+
const logAddr = log.line && log.column ? '\t@' + log.line + ':' + log.column : '';
|
|
121
|
+
if (suppresslog) return;
|
|
122
|
+
if (log.reason.startsWith('ERROR')) {
|
|
123
|
+
colorconsole.error(loader.resourcePath, log.reason + logAddr);
|
|
124
|
+
} else {
|
|
125
|
+
colorconsole.warn(loader.resourcePath, log.reason + logAddr);
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 创建任意深度的路径的文件夹
|
|
133
|
+
* @param dirname
|
|
134
|
+
* @returns {boolean}
|
|
135
|
+
*/
|
|
136
|
+
function mkdirsSync(dirname) {
|
|
137
|
+
if (_fs.default.existsSync(dirname)) {
|
|
138
|
+
return true;
|
|
139
|
+
} else {
|
|
140
|
+
if (mkdirsSync(_path.default.dirname(dirname))) {
|
|
141
|
+
_fs.default.mkdirSync(dirname);
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* 获取服务器端的IP
|
|
149
|
+
*/
|
|
150
|
+
function getIPv4IPAddress() {
|
|
151
|
+
const ifaces = _os.default.networkInterfaces();
|
|
152
|
+
let result;
|
|
153
|
+
for (const prop in ifaces) {
|
|
154
|
+
if (Object.prototype.hasOwnProperty.call(ifaces, prop)) {
|
|
155
|
+
const iface = ifaces[prop];
|
|
156
|
+
iface.every(eachAlias => {
|
|
157
|
+
if (eachAlias.family === 'IPv4' && !eachAlias.internal && eachAlias.address !== '127.0.0.1') {
|
|
158
|
+
result = eachAlias;
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
return true;
|
|
162
|
+
});
|
|
163
|
+
if (result !== void 0) {
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return result && result.address;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* 取得server端的IP和port
|
|
173
|
+
* @param port {number}
|
|
174
|
+
* @returns {string}
|
|
175
|
+
*/
|
|
176
|
+
function getServerIPAndPort(port) {
|
|
177
|
+
return (getIPv4IPAddress() || '127.0.0.1') + `${port === 80 ? '' : ':' + port}`;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* 获取默认的服务器地址
|
|
182
|
+
*/
|
|
183
|
+
function getDefaultServerHost() {
|
|
184
|
+
return getServerIPAndPort(_config.default.server.port);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* 获取客户端ip
|
|
189
|
+
* @param req node Http IncomingRequest Object
|
|
190
|
+
* @returns {any|*|string}
|
|
191
|
+
*/
|
|
192
|
+
function getClientIPAddress(req) {
|
|
193
|
+
const ip = req.headers['x-forwarded-for'] || req.connection && req.connection.remoteAddress || req.socket && req.socket.remoteAddress || req.connection && req.connection.socket && req.connection.socket.remoteAddress;
|
|
194
|
+
return stripPrefixForIPV4MappedIPV6Address(ip);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* 检测可能是IpV4-mapped IpV6 格式的ip
|
|
199
|
+
* - https://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses
|
|
200
|
+
*
|
|
201
|
+
* @param ip IpV4-mapped IpV6 string
|
|
202
|
+
* @returns {*}
|
|
203
|
+
*/
|
|
204
|
+
function stripPrefixForIPV4MappedIPV6Address(ip) {
|
|
205
|
+
if (/^::1$/.test(ip)) {
|
|
206
|
+
ip = '127.0.0.1';
|
|
207
|
+
}
|
|
208
|
+
if (/^::.{0,4}:(\d{1,3}\.){3}\d{1,3}/.test(ip)) {
|
|
209
|
+
ip = ip.replace(/^.*:/, '');
|
|
210
|
+
}
|
|
211
|
+
return ip;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* 命令行输出二维码
|
|
216
|
+
* @param text
|
|
217
|
+
*/
|
|
218
|
+
function outputQRCodeOnTerminal(text) {
|
|
219
|
+
console.info(`\nGenerate QR code for HTTP server: ${text}`);
|
|
220
|
+
_qrcodeTerminal.default.generate(text, {
|
|
221
|
+
small: true
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* 相对工作目录的路径
|
|
227
|
+
*
|
|
228
|
+
* @param {String} filepath - 输入路径
|
|
229
|
+
* @returns {String} - 输出相对路径
|
|
230
|
+
*/
|
|
231
|
+
function relateCwd(filepath) {
|
|
232
|
+
const CWD = _config.default.projectPath;
|
|
233
|
+
return _path.default.relative(CWD, filepath);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* 比较两个对象是否相同
|
|
238
|
+
* @param o1
|
|
239
|
+
* @param o2
|
|
240
|
+
* @param fn {Function} 对比函数
|
|
241
|
+
*/
|
|
242
|
+
function equals(o1, o2, fn, ...args) {
|
|
243
|
+
if (fn) {
|
|
244
|
+
const fnRet = fn(o1, o2, ...args);
|
|
245
|
+
if (fnRet) {
|
|
246
|
+
return true;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
const toStr1 = Object.prototype.toString.call(o1);
|
|
250
|
+
const toStr2 = Object.prototype.toString.call(o2);
|
|
251
|
+
if (toStr1 !== toStr2) {
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
if (toStr1 === '[object Null]' || toStr1 === '[object Undefined]') {
|
|
255
|
+
return true;
|
|
256
|
+
}
|
|
257
|
+
if (toStr1 !== '[object Object]' && toStr1 !== '[object Array]') {
|
|
258
|
+
return Object(o1).toString() === Object(o2).toString();
|
|
259
|
+
}
|
|
260
|
+
const keyMap = {};
|
|
261
|
+
Object.keys(o1).forEach(k => keyMap[k] = true);
|
|
262
|
+
Object.keys(o2).forEach(k => keyMap[k] = true);
|
|
263
|
+
const keyList = Object.keys(keyMap);
|
|
264
|
+
for (let i = 0; i < keyList.length; i++) {
|
|
265
|
+
const keyName = keyList[i];
|
|
266
|
+
const isEqual = equals(o1[keyName], o2[keyName], fn, keyName);
|
|
267
|
+
if (!isEqual) {
|
|
268
|
+
return false;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return true;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* 扩展对象属性
|
|
276
|
+
* @param dest
|
|
277
|
+
* @param src
|
|
278
|
+
*/
|
|
279
|
+
function extend(target, ...src) {
|
|
280
|
+
if (typeof Object.assign === 'function') {
|
|
281
|
+
Object.assign(target, ...src);
|
|
282
|
+
} else {
|
|
283
|
+
const first = src.shift();
|
|
284
|
+
// 覆盖旧值
|
|
285
|
+
for (const key in first) {
|
|
286
|
+
target[key] = first[key];
|
|
287
|
+
}
|
|
288
|
+
if (src.length) {
|
|
289
|
+
extend(target, ...src);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return target;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* 简单的模板字符串渲染函数
|
|
297
|
+
*
|
|
298
|
+
* @param {String} tpl - 模板字符串
|
|
299
|
+
* @param {Object} data - 渲染数据
|
|
300
|
+
* @returns {String}
|
|
301
|
+
*/
|
|
302
|
+
function renderString(tpl, data) {
|
|
303
|
+
return tpl.replace(/{{(.*?)}}/gm, (_, key) => {
|
|
304
|
+
key = key.trim();
|
|
305
|
+
return data[key] !== undefined ? data[key] : key;
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
function KnownError(message) {
|
|
309
|
+
const err = new Error(message);
|
|
310
|
+
err.name = 'KnownError';
|
|
311
|
+
err.__KNOWN = true;
|
|
312
|
+
return err;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* 获取并设置自定义配置项
|
|
317
|
+
* @param {String} projectPath - 项目路径
|
|
318
|
+
* @param {String} port - server端口
|
|
319
|
+
*/
|
|
320
|
+
function setCustomConfig(projectPath, port) {
|
|
321
|
+
projectPath = _config.default.projectPath = projectPath || _config.default.projectPath;
|
|
322
|
+
const defaultConfig = 'quickapp.config.js';
|
|
323
|
+
const defaultConfigPath = _path.default.join(projectPath, defaultConfig);
|
|
324
|
+
if (_fs.default.existsSync(defaultConfigPath)) {
|
|
325
|
+
let customConf = {};
|
|
326
|
+
try {
|
|
327
|
+
customConf = require(defaultConfigPath);
|
|
328
|
+
} catch (err) {
|
|
329
|
+
colorconsole.error(`Error reading project custom configuration file: ${err.message}`);
|
|
330
|
+
}
|
|
331
|
+
// 保证 customConf server 字段无内容也可保证默认值
|
|
332
|
+
const server = Object.assign(_config.default.server, customConf.server);
|
|
333
|
+
Object.assign(_config.default, customConf, {
|
|
334
|
+
server
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
port && (_config.default.server.port = port);
|
|
338
|
+
}
|
|
339
|
+
const illegalExtsList = ['.css', '.less', '.scss', '.styl', '.sass', '.log', '.json', '.js'].map(e => 'app' + e);
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* 获取当前项目所使用的DSL
|
|
343
|
+
* @param projectPath
|
|
344
|
+
*/
|
|
345
|
+
function getProjectDslName(projectPath) {
|
|
346
|
+
// 根据项目中src/app的后缀名判断
|
|
347
|
+
const srcFiles = _fs.default.readdirSync(_path.default.join(projectPath, _config.default.sourceRoot));
|
|
348
|
+
const appFileNames = srcFiles.filter(f => /^app\..*/.test(f) && !illegalExtsList.includes(f));
|
|
349
|
+
let appFileExt;
|
|
350
|
+
if (appFileNames && appFileNames[0]) {
|
|
351
|
+
appFileExt = _path.default.extname(appFileNames[0]).slice(1);
|
|
352
|
+
colorconsole.info(`Get the app file suffix: ${appFileExt}`);
|
|
353
|
+
} else {
|
|
354
|
+
colorconsole.error(`Cannot get the correct app file suffix`);
|
|
355
|
+
}
|
|
356
|
+
return appFileExt === 'ux' ? 'xvm' : appFileExt;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* 获取手机设备信息(1020新增接口)
|
|
361
|
+
* @param client {{ ip, port }}
|
|
362
|
+
* @param callback
|
|
363
|
+
*/
|
|
364
|
+
function getDeviceInfo(client, callback) {
|
|
365
|
+
const req = _http.default.request({
|
|
366
|
+
path: '/deviceinfo',
|
|
367
|
+
host: client.ip,
|
|
368
|
+
port: client.port,
|
|
369
|
+
timeout: 3000
|
|
370
|
+
}, res => {
|
|
371
|
+
res.on('data', data => {
|
|
372
|
+
callback(null, JSON.parse(data));
|
|
373
|
+
});
|
|
374
|
+
}).on('error', err => {
|
|
375
|
+
callback(err);
|
|
376
|
+
}).on('timeout', function () {
|
|
377
|
+
// abort方法会触发error事件
|
|
378
|
+
req.abort();
|
|
379
|
+
});
|
|
380
|
+
req.end();
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* 读取一份 json 内容
|
|
385
|
+
*
|
|
386
|
+
* @param {String} jsonpath - json 路径
|
|
387
|
+
* @returns {Object} json
|
|
388
|
+
*/
|
|
389
|
+
function readJson(jsonpath) {
|
|
390
|
+
try {
|
|
391
|
+
return JSON.parse(_fs.default.readFileSync(jsonpath).toString());
|
|
392
|
+
} catch (err) {
|
|
393
|
+
if (err instanceof SyntaxError) {
|
|
394
|
+
throw new SyntaxError(`Failed to parse ${jsonpath}, format error`);
|
|
395
|
+
} else if (err.message.startsWith('ENOENT:')) {
|
|
396
|
+
err.message = `Failed to read ${jsonpath}, the file cannot be found`;
|
|
397
|
+
throw err;
|
|
398
|
+
} else {
|
|
399
|
+
err.message = `Failed to read ${jsonpath} ${err.message}`;
|
|
400
|
+
throw err;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* 获取 manifest.json 内容
|
|
407
|
+
*
|
|
408
|
+
* @returns {Object} json
|
|
409
|
+
*/
|
|
410
|
+
function getManifest() {
|
|
411
|
+
try {
|
|
412
|
+
// 需要特殊处理打包mina-h5时,manifest.json文件在根目录下
|
|
413
|
+
let manifestFile = _path.default.resolve(_config.default.projectPath, 'manifest.json');
|
|
414
|
+
if (!_fs.default.existsSync(manifestFile)) {
|
|
415
|
+
manifestFile = _path.default.resolve(_config.default.projectPath, _config.default.sourceRoot, 'manifest.json');
|
|
416
|
+
}
|
|
417
|
+
const manifest = readJson(manifestFile);
|
|
418
|
+
return manifest;
|
|
419
|
+
} catch (err) {
|
|
420
|
+
throw err;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* 获取识别为.ux的文件后缀列表
|
|
426
|
+
* @param {*} withDot 扩展名是否包含.
|
|
427
|
+
* @returns string[]
|
|
428
|
+
*/
|
|
429
|
+
function getUxExtensionList(withDot = true) {
|
|
430
|
+
const result = ['ux', 'mix', 'hml'];
|
|
431
|
+
if (withDot) {
|
|
432
|
+
return result.map(item => `.${item}`);
|
|
433
|
+
}
|
|
434
|
+
return result;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* 合并json文件的内容,把fromPath的内容合并到toPath
|
|
439
|
+
* @param {*} fromPath
|
|
440
|
+
* @param {*} toPath
|
|
441
|
+
*/
|
|
442
|
+
function mergeJsonFile(fromPath, toPath) {
|
|
443
|
+
if (!_fs.default.existsSync(fromPath) || !_fs.default.existsSync(toPath)) {
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// 读取两个文件的数据-->转换成json-->frompath的json属性覆盖topath的同名属性
|
|
448
|
+
const fromStr = _fs.default.readFileSync(fromPath).toString();
|
|
449
|
+
const toStr = _fs.default.readFileSync(toPath).toString();
|
|
450
|
+
try {
|
|
451
|
+
const fromObj = JSON.parse(fromStr);
|
|
452
|
+
const toObj = JSON.parse(toStr);
|
|
453
|
+
const result = Object.assign({}, toObj, fromObj);
|
|
454
|
+
_fs.default.writeFileSync(toPath, JSON.stringify(result, null, 2));
|
|
455
|
+
} catch (error) {
|
|
456
|
+
colorconsole.error(`Parse JSON Error: ${error.message}`);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* 通过tag过滤内容
|
|
462
|
+
* @param {string} source 源文本
|
|
463
|
+
* @param {(tagName)=>boolean} includeFun 过滤方法,返回true表示保留
|
|
464
|
+
*
|
|
465
|
+
* @return string
|
|
466
|
+
*/
|
|
467
|
+
function filterContentByTag(source, includeFun) {
|
|
468
|
+
if (!source) {
|
|
469
|
+
return '';
|
|
470
|
+
}
|
|
471
|
+
const nodes = _parse.default.parseFragment(source, {
|
|
472
|
+
treeAdapter: _parse.default.treeAdapters.default,
|
|
473
|
+
locationInfo: true
|
|
474
|
+
}).childNodes;
|
|
475
|
+
if (!nodes || !nodes.length) {
|
|
476
|
+
return '';
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
// 过滤childrenNodes
|
|
480
|
+
const effectNodes = nodes.filter(item => includeFun(item.nodeName));
|
|
481
|
+
|
|
482
|
+
// 生成html
|
|
483
|
+
const serializer = new _index.default({
|
|
484
|
+
childNodes: effectNodes
|
|
485
|
+
});
|
|
486
|
+
serializer.serialize();
|
|
487
|
+
return serializer.html;
|
|
488
|
+
}
|
|
2
489
|
//# sourceMappingURL=index.js.map
|