From 04abf7168fa9567b935785be96b44d50e65b82ec Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Tue, 9 Apr 2024 16:02:53 +0800 Subject: [PATCH] fix: no backslash --- lib/plugins/tag/include_code.ts | 20 ++++++++------------ test/scripts/tags/include_code.ts | 2 +- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/lib/plugins/tag/include_code.ts b/lib/plugins/tag/include_code.ts index b2f09e972b..fb6322f5e9 100644 --- a/lib/plugins/tag/include_code.ts +++ b/lib/plugins/tag/include_code.ts @@ -1,4 +1,5 @@ -import { basename, extname, join, posix } from 'path'; +import { basename, extname, join } from 'path'; +import { url_for } from 'hexo-util'; import type Hexo from '../../hexo'; const rCaptionTitleFile = /(.*)?(?:\s+|^)(\/*\S+)/; @@ -13,11 +14,6 @@ const rTo = /\s*to:(\d+)/i; * {% include_code [title] [lang:language] path/to/file %} */ -const escapeBackslash = path => { - // Replace backslashes on Windows - return path.replace(/\\/g, '/'); -}; - export = (ctx: Hexo) => function includeCodeTag(args: string[]) { let codeDir = ctx.config.code_dir; let arg = args.join(' '); @@ -51,21 +47,21 @@ export = (ctx: Hexo) => function includeCodeTag(args: string[]) { // If the language is not defined, use file extension instead lang = lang || extname(path).substring(1); - const src = escapeBackslash(join(codeDir, path)); - - // If the title is not defined, use file name instead - const title = match[1] || basename(path); - const caption = `${title}view raw`; + const source = join(codeDir, path); // Prevent path traversal: https://github.com/hexojs/hexo/issues/5250 const Page = ctx.model('Page'); - const doc = Page.findOne({ source: src }); + const doc = Page.findOne({ source }); if (!doc) return; let code = doc.content; const lines = code.split('\n'); code = lines.slice(from, to).join('\n').trim(); + // If the title is not defined, use file name instead + const title = match[1] || basename(path); + const caption = `${title}view raw`; + if (ctx.extend.highlight.query(ctx.config.syntax_highlighter)) { const options = { lang, diff --git a/test/scripts/tags/include_code.ts b/test/scripts/tags/include_code.ts index e2c558aa93..7d104fbf80 100644 --- a/test/scripts/tags/include_code.ts +++ b/test/scripts/tags/include_code.ts @@ -16,7 +16,7 @@ describe('include_code', () => { const defaultCfg = JSON.parse(JSON.stringify(hexo.config)); const fixture = [ - 'if (tired && night){', + 'if (tired && night) {', ' sleep();', '}' ].join('\n');