Skip to content

Conversation

@D-Sketon
Copy link
Member

What does it do?

try to fix #5479 and #5486

ref #5479 (comment)

Since the current model doesn't satisfy include_code, I experimentally added the Code Model

Screenshots

Pull request tasks

  • Add test cases for the changes.
  • Passed the CI test.

@github-actions
Copy link

How to test

git clone -b fix/code https://github.com/D-Sketon/hexo.git
cd hexo
npm install
npm test

@D-Sketon D-Sketon linked an issue Feb 13, 2025 that may be closed by this pull request
5 tasks
@D-Sketon
Copy link
Member Author

I'm not sure the files in code_dir need to be written to the public folder

@coveralls
Copy link

coveralls commented Feb 15, 2025

Pull Request Test Coverage Report for Build 18804435888

Details

  • 100 of 100 (100.0%) changed or added relevant lines in 9 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.005%) to 99.534%

Totals Coverage Status
Change from base Build 18804356593: 0.005%
Covered Lines: 10030
Relevant Lines: 10077

💛 - Coveralls

@D-Sketon D-Sketon marked this pull request as ready for review February 15, 2025 06:54
@D-Sketon D-Sketon requested a review from a team March 2, 2025 04:30
const Code = new warehouse.Schema<CodeSchema>({
_id: { type: String, required: true },
path: { type: String, required: true },
slug: { type: String, required: true },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the same value is already set in path, I don't think slug is necessary. Though the difference may be slight, reducing the elements in Schema<CodeSchema> should have a positive effect on performance.

However, if we remove the slug, it could potentially be a breaking change since there may be users referencing it in custom scripts or etc..., so it might require an announcement.

@D-Sketon D-Sketon requested a review from Copilot July 25, 2025 04:07
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a new Code model and associated functionality to support the include_code tag in Hexo. The changes address issues #5479 and #5486 by creating a separate processing pipeline for code files that doesn't interfere with the existing page/asset processing.

  • Adds a new Code model with processor, generator, and comprehensive test coverage
  • Updates the include_code tag to use the Code model instead of the Page model
  • Modifies asset processor to exclude code directory files from asset processing

Reviewed Changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
lib/models/code.ts Defines the new Code model schema with required fields and virtual source property
lib/plugins/processor/code.ts Processes files in the code directory and manages Code model entries
lib/plugins/generator/code.ts Generates code files and handles cleanup of non-existent files
lib/plugins/tag/include_code.ts Updates to use Code model instead of Page model for finding code files
lib/plugins/processor/asset.ts Excludes code directory files from asset processing
lib/types.ts Adds CodeSchema interface definition
test/scripts/tags/include_code.ts Adds test for .j2 file extension support
test/scripts/processors/code.ts Comprehensive tests for code processor functionality
test/scripts/models/code.ts Tests for Code model validation and behavior
test/scripts/generators/code.ts Tests for code generator functionality
Comments suppressed due to low confidence (2)

test/scripts/tags/include_code.ts:10

  • [nitpick] The test suite name 'include_code_js' is misleading as it doesn't specifically test JavaScript files. Consider renaming to 'include_code' or 'include_code_original' to better reflect what it tests.
describe('include_code_js', () => {

test/scripts/tags/include_code.ts:228

  • [nitpick] The test suite name 'include_code_j2' is inconsistent with the naming pattern. Consider using a more descriptive name like 'include_code - jinja2 files' to clarify the purpose.
describe('include_code_j2', () => {

Comment on lines +13 to +15
return Promise.filter(this.model('Code').toArray(), (code: Document<CodeSchema>) => exists(code.source).tap(exist => {
if (!exist) return code.remove();
})).map((code: Document<CodeSchema>) => {
Copy link

Copilot AI Jul 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation checks file existence for every code file on each generation. Consider implementing a more efficient approach that only checks existence when files are modified or caches the results.

Suggested change
return Promise.filter(this.model('Code').toArray(), (code: Document<CodeSchema>) => exists(code.source).tap(exist => {
if (!exist) return code.remove();
})).map((code: Document<CodeSchema>) => {
const fileExistenceCache: Record<string, boolean> = {};
return Promise.filter(this.model('Code').toArray(), (code: Document<CodeSchema>) => {
if (!code.modified && fileExistenceCache[code.source] !== undefined) {
// Use cached result if file is not modified
return Promise.resolve(fileExistenceCache[code.source]);
}
// Check file existence and update cache
return exists(code.source).tap(exist => {
fileExistenceCache[code.source] = exist;
if (!exist) return code.remove();
});
}).map((code: Document<CodeSchema>) => {

Copilot uses AI. Check for mistakes.
Comment on lines +12 to +24
function codeGenerator(this: Hexo): Promise<any[]> {
return Promise.filter(this.model('Code').toArray(), (code: Document<CodeSchema>) => exists(code.source).tap(exist => {
if (!exist) return code.remove();
})).map((code: Document<CodeSchema>) => {
const { path } = code;
const data: CodeData = {
modified: code.modified,
data: code.content
};

return { path, data };
});

Copy link

Copilot AI Jul 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The returned object structure is inconsistent with other generators. Consider adding a comment explaining why this generator returns a different structure or align it with the BaseGeneratorReturn interface.

Suggested change
function codeGenerator(this: Hexo): Promise<any[]> {
return Promise.filter(this.model('Code').toArray(), (code: Document<CodeSchema>) => exists(code.source).tap(exist => {
if (!exist) return code.remove();
})).map((code: Document<CodeSchema>) => {
const { path } = code;
const data: CodeData = {
modified: code.modified,
data: code.content
};
return { path, data };
});
interface BaseGeneratorReturn {
path: string;
data: {
modified: boolean;
data: string;
};
}
function codeGenerator(this: Hexo): Promise<BaseGeneratorReturn[]> {
return Promise.filter(this.model('Code').toArray(), (code: Document<CodeSchema>) => exists(code.source).tap(exist => {
if (!exist) return code.remove();
})).map((code: Document<CodeSchema>) => {
const { path } = code;
const data: BaseGeneratorReturn['data'] = {
modified: code.modified,
data: code.content
};
return { path, data };
});

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

include_code tag is broken in Hexo 7.2.0 .j2 file in code_dir causing rendering error

3 participants