Skip to content

Commit

Permalink
refactor: 删除exturl相关功能
Browse files Browse the repository at this point in the history
  • Loading branch information
zkz098 committed Dec 2, 2023
1 parent f208f48 commit b76ce07
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 82 deletions.
16 changes: 0 additions & 16 deletions scripts/filters/post.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
/* global hexo */

hexo.extend.filter.register('after_post_render', (data) => {
const { config } = hexo

// 使用正则表达式将<img>标签的src属性替换为data-src属性
data.content = data.content.replace(/(<img[^>]*) src=/img, '$1 data-src=')

// 从config对象中获取站点URL的主机名
const siteHost:string = new URL(config.url).hostname || config.url
// 使用正则表达式将<a>标签转换为<span>标签,并添加自定义属性以记录链接URL
data.content = data.content.replace(/<a[^>]* href="([^"]+)"[^>]*>([^<]*)<\/a>/img, (match:string, href:string, html:string) => {
if (!href) return match // 如果href不存在,则返回原始的<a>标签

// 创建一个新的URL对象来检查链接URL的主机名是否与站点URL相同
const link = new URL(href, config.url)
if (!link.protocol || link.hostname === siteHost) return match

// 将链接URL转换为Base64编码的字符串,并将其添加到新的<span>标签中作为自定义属性
return `<span class="exturl" data-url="${Buffer.from(href).toString('base64')}">${html}</span>`
})
}, 0)
45 changes: 1 addition & 44 deletions scripts/helpers/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,60 +57,17 @@ hexo.extend.helper.register('_url', function (path, text, options = {}) {
// 如果未提供URL路径,则返回
if (!path) { return }

// 获取hexo配置和URL路径信息
const { config } = this
const data = new URL(path, hexo.config.url)
const siteHost = new URL(config.url).hostname || config.url

// 获取主题配置
const theme = hexo.theme.config
let exturl = ''
let tag = 'a'
let attrs: { class: string; 'data-url': any; [index:string]:any } = { href: url_for.call(this, path), class: undefined, external: undefined, rel: undefined, 'data-url': undefined }

// 如果启用了 `exturl`,则只为外部链接设置spanned链接。
if (theme.exturl && data.protocol && data.hostname !== siteHost) {
tag = 'span'
exturl = 'exturl'
// 编码URL字符串,并将其存储在数据属性中。
const encoded = Buffer.from(path).toString('base64')

attrs = {
class: exturl,
'data-url': encoded
}
}

for (const key in options) {
/**
* 如果选项包含 `class` 属性,则将其添加到 `exturl` 类中(如果启用了 `exturl` 选项)。
* 否则,将其添加到属性集中。
*/
if (exturl !== '' && key === 'class') {
attrs[key] += ' ' + options[key]
} else {
attrs[key] = options[key]
}
attrs[key] = options[key]
}

if (attrs.class && Array.isArray(attrs.class)) {
attrs.class = attrs.class.join(' ')
}

// 如果是外部链接,则重写属性
if (data.protocol && data.hostname !== siteHost) {
attrs.external = null

if (!theme.exturl) {
// 仅需要为简单链接重写/添加属性。
attrs.rel = 'noopener'
attrs.target = '_blank'
} else {
// 在主菜单中移除 `exturl` 的 rel 属性。
attrs.rel = null
}
}

// 返回HTML标记字符串
return htmlTag(tag, attrs, decodeURI(text), false)
})
Expand Down
20 changes: 0 additions & 20 deletions source/js/_app/page/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,3 @@ export const cardActive = () => {
})
})
}

export const registerExtURL = () => {
$dom.each('span.exturl', (element) => {
const link = <HTMLAnchorElement>document.createElement('a')
// https://stackoverflow.com/questions/30106476/using-javascripts-atob-to-decode-base64-doesnt-properly-decode-utf-8-strings

link.href = decodeURIComponent(window.atob(element.dataset.url).split('').map((c) => {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
}).join(''))
link.rel = 'noopener external nofollow noreferrer'
link.target = '_blank'
link.className = element.className
link.title = element.title || element.innerText
link.innerHTML = element.innerHTML
if (element.dataset.backgroundImage) {
link.dataset.backgroundImage = element.dataset.backgroundImage
}
element.parentNode.replaceChild(link, element)
})
}
3 changes: 1 addition & 2 deletions source/js/_app/pjax/refresh.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { $dom } from '../library/dom'
import { cardActive, registerExtURL } from '../page/common'
import { cardActive } from '../page/common'
import { postBeauty } from '../page/post'
import { pageScroll, transition } from '../library/anime'
import { vendorCss, vendorJs } from '../library/loadFile'
Expand Down Expand Up @@ -54,7 +54,6 @@ export const siteRefresh = (reload) => {
sideBarTab()
sidebarTOC()

registerExtURL()
postBeauty()
tabFormat()
if (typeof mediaPlayer !== 'undefined') {
Expand Down

0 comments on commit b76ce07

Please sign in to comment.