-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
397 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright (c) 2023, Terwer . All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. Terwer designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Terwer in the LICENSE file that accompanied this code. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com | ||
* or visit www.terwer.space if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
import * as cheerio from "cheerio" | ||
import { createAppLogger } from "~/common/appLogger" | ||
import { Cheerio } from "cheerio" | ||
|
||
/** | ||
* DOM 相关操作钩子 | ||
*/ | ||
export const useDom = () => { | ||
const logger = createAppLogger("use-dom") | ||
|
||
/** | ||
* 获取HTML代码中第一个<img>元素的src属性值 | ||
* | ||
* @param {string} html - HTML代码 | ||
* @returns {string} 第一个<img>元素的src属性值,如果不存在则返回空字符串 | ||
*/ | ||
const getFirstImageSrc = (html: string): string => { | ||
logger.info(`getFirstImageSrc called with argument ${html}`) // 记录日志 | ||
|
||
// 使用 cheerio 加载 HTML 代码 | ||
const $ = cheerio.load(html) | ||
|
||
// 获取第一个<img>元素 | ||
const firstImg = $("img").first() | ||
if (!firstImg.length) { | ||
// 没有找到<img>元素,返回空字符串 | ||
return "" | ||
} | ||
|
||
// 返回<img>元素的src属性 | ||
const src = firstImg.attr("src") || "" | ||
logger.info(`getFirstImageSrc returns ${src}`) // 记录日志 | ||
return src | ||
} | ||
|
||
/** | ||
* 在 HTML 中添加资源路径前缀 | ||
* | ||
* @param {string} html - HTML 代码 | ||
* @returns {string} 添加前缀后的 HTML 代码 | ||
*/ | ||
const addAssetsPrefix = (html: string): string => { | ||
const env = useRuntimeConfig() | ||
const imagePrefix = env.public.siyuanApiUrl | ||
logger.debug("imagePrefix=>", imagePrefix) | ||
|
||
// 使用 cheerio 加载 HTML 代码 | ||
const $ = cheerio.load(html) | ||
const images = $("img") | ||
for (const image of images) { | ||
const src = image.attribs["src"] | ||
const newImageUrl = [imagePrefix, src].join("/") | ||
logger.debug("image src=>", src) | ||
logger.debug("newImageUrl=>", newImageUrl) | ||
|
||
image.attribs["src"] = newImageUrl | ||
} | ||
|
||
// 将修改后的 DOM 结构重新序列化成字符串并返回 | ||
return $.html() | ||
} | ||
|
||
return { getFirstImageSrc, addAssetsPrefix } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright (c) 2023, Terwer . All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. Terwer designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Terwer in the LICENSE file that accompanied this code. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com | ||
* or visit www.terwer.space if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
import { createAppLogger } from "~/common/appLogger" | ||
import { StrUtil } from "zhi-common" | ||
|
||
/** | ||
* 页面渲染插件(图片、链接、公式等) - 客户端 | ||
* https://github.com/nuxt/nuxt/issues/13382 | ||
* client = browser only | ||
* | ||
* @author terwer | ||
* @version 1.0.0 | ||
* @since 0.0.1 | ||
*/ | ||
export default defineNuxtPlugin(({ vueApp }) => { | ||
const logger = createAppLogger("renderer-client-plugin") | ||
|
||
vueApp.directive("beauty", (el: HTMLElement) => { | ||
if (process.env.SSR === "true") { | ||
logger.warn("SSR is enabled, render is dealed with nitro, so the client conversion is ignored") | ||
return | ||
} | ||
|
||
logger.info("render:html Start dealing with resource images on client", el) | ||
// assets | ||
const imgs = el.querySelectorAll("img") | ||
if (imgs && imgs.length > 0) { | ||
imgs.forEach((img) => { | ||
const src = img.getAttribute("src") ?? "" | ||
if (src.indexOf("assets") > -1) { | ||
const env = useRuntimeConfig() | ||
const imagePrefix = env.public.siyuanApiUrl | ||
const origin = window.location.origin | ||
const baseUrl = StrUtil.isEmptyString(imagePrefix) ? origin : imagePrefix | ||
const imgUrl = [baseUrl, src].join("/") | ||
|
||
img.setAttribute("src", imgUrl) | ||
} | ||
}) | ||
logger.info("The local image has been processed and the picture display has been repaired.") | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) 2023, Terwer . All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. Terwer designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Terwer in the LICENSE file that accompanied this code. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com | ||
* or visit www.terwer.space if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
import { createAppLogger } from "~/common/appLogger" | ||
import { useHljs } from "~/plugins/hljs/useHljs" | ||
|
||
// stub - block nuxt getSSRProps error | ||
export default defineNuxtPlugin(({ vueApp }) => { | ||
vueApp.directive("beauty", {}) | ||
}) |
Oops, something went wrong.