Skip to content

Commit

Permalink
feat: 剔除无用标签, 特别是script, 避免在渲染时, 阻塞页面加载
Browse files Browse the repository at this point in the history
  • Loading branch information
yangmingming committed Oct 27, 2024
1 parent a0feb92 commit 74c8374
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/command/generate/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,16 @@ class GenerateBase extends Base {
processContent(content: string) {
let that = this
// 删除noscript标签内的元素
function removeNoScript(rawHtml: string) {
function removeUselessEle(rawHtml: string) {
// 规范br标签
rawHtml = _.replace(rawHtml, /<\/br>/g, '')
rawHtml = _.replace(rawHtml, /<br>/g, '<br/>')
// 修复跳转链接
rawHtml = _.replace(rawHtml, /href="\/\/link.zhihu.com'/g, 'href="https://link.zhihu.com') // 修复跳转链接
// 移除noscript标签
rawHtml = _.replace(rawHtml, /\<noscript\>.*?\<\/noscript\>/g, '')
// 移除script标签
rawHtml = _.replace(rawHtml, /\<script .*?\<\/script\>/g, '')
return rawHtml
}

Expand Down Expand Up @@ -176,7 +181,7 @@ class GenerateBase extends Base {
let processedHtml = strMergeList.join('')
return processedHtml
}
content = removeNoScript(content)
content = removeUselessEle(content)
let tinyContentList = content.split(`<div data-key='single-page'`).map(value => {
return replaceImgSrc(value)
})
Expand Down

0 comments on commit 74c8374

Please sign in to comment.