加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
simplifyFile.js 3.63 KB
一键复制 编辑 原始数据 按行查看 历史
xuiang 提交于 2022-08-15 00:31 . init nodeTry
/*
* @Author: xuiang 1477388171@QQ.com
* @Date: 2022-08-13 20:04:26
* @LastEditors: xuiang 1477388171@QQ.com
* @LastEditTime: 2022-08-14 10:18:44
* @FilePath: \node try\simplifyFile.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
const fs = require('fs')
const path = require('path')
let fileName = '/complex.html'
let fullPath = path.join(__dirname, fileName)
console.log(fullPath);
function getStyle(str) {
let obj = {
stringWithoutStyle: '',//没有style标签后字符串
rawString: str,//原来的字符串
innerStyle: '',//style标签内的内容
startIndex: 0,//找到style开始的索引
endIndex: 0,//找到style结束的索引
}
let matchStyle = /(<style>)([\s|\S]*)(<\/style>)/g;
let result = matchStyle.exec(str)
obj.endIndex = matchStyle.lastIndex
obj.startIndex = result.index
obj.innerStyle = result[2]
obj.stringWithoutStyle = str.substring(0,obj.startIndex) + str.substring(obj.endIndex)
return obj
}
function getScript(str) {
let obj = {
stringWithoutScript: "", //没有Script标签后字符串
rawString: str, //原来的字符串
innerScript: "", //Script标签内的内容
startIndex: 0, //找到Script开始的索引
endIndex: 0, //找到Script结束的索引
};
let matchScript = /(<script>)([\s|\S]*)(<\/script>)/g;
let result = matchScript.exec(str);
obj.endIndex = matchScript.lastIndex;
obj.startIndex = result.index;
obj.innerScript = result[2];
obj.stringWithoutScript =
str.substring(0, obj.startIndex) + str.substring(obj.endIndex);
return obj;
}
function insertStyleLink(atString, path, fileNameWithoutExt,startIndex) {
let link = `<link rel="stylesheet" href="${path}${fileNameWithoutExt}.css"/>`;
let stringIncludeStyle =
atString.substring(0, startIndex) + link + atString.substring(startIndex);
return stringIncludeStyle;
}
function insertScriptLink(
atString,
path,
fileNameWithoutExt,
startIndex
) {
// 加上如果没有startIndex时的逻辑
let script = `<script src="${path}${fileNameWithoutExt}.js"></script>`;
let stringIncludeScript = atString.substring(0, startIndex) + script + atString.substring(startIndex)
return stringIncludeScript
}
function createFile(fileNameWithExt, content) {
fs.writeFile(path.join(__dirname, `/${fileNameWithExt}`), content, 'utf-8', err => {
if (err) {
console.log(err)
}
});
}
fs.readFile(fullPath, 'utf-8', (err, data) => {
if (err) {
console.log(err)
} else {
debugger;
let excludeStyle = getStyle(data);
let insertedLink = insertStyleLink(
excludeStyle.stringWithoutStyle,
"./",
"index",
excludeStyle.startIndex
);
let excludeScript = getScript(insertedLink);
let insertedScript = insertScriptLink(
excludeScript.stringWithoutScript,
"./",
"index",
excludeScript.startIndex
);
// 可以使用str.replace方法直接提换成link与src,而不是像上面一样逐个拼接
let result = data
.replace(
/(<style>)([\s|\S]*)(<\/style>)/g,
`<link rel="stylesheet" href="./index.css"/>`
)
.replace(
/(<script>)([\s|\S]*)(<\/script>)/g,
`<script src="./index.js"></script>`
);
createFile("simplify.html", result);
createFile("index.css", excludeStyle.innerStyle);
createFile("index.js", excludeScript.innerScript);
// writeFile不能创建目录,但是能创建文件夹
}
})
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化