-
-
Notifications
You must be signed in to change notification settings - Fork 38
refactor(model): add Code model for include_code tag #5807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
D-Sketon
wants to merge
21
commits into
hexojs:master
Choose a base branch
from
D-Sketon:fix/code
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+562
−6
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
798afce
refactor(model): add Code model for include_code tag
D-Sketon caf9d9c
add test
D-Sketon ae5fae7
fix(page): exclude code directory from processing
D-Sketon c03d4ce
Merge branch 'master' into fix/code
D-Sketon 98cf8c5
Merge branch 'master' into fix/code
D-Sketon b1d66ef
Merge branch 'master' into fix/code
D-Sketon 0f5713d
Merge branch 'master' into fix/code
D-Sketon d1bbd80
Merge branch 'master' into fix/code
D-Sketon 80ec349
Merge branch 'master' into fix/code
D-Sketon 3e4bd21
Merge branch 'master' into fix/code
D-Sketon 724853f
Merge branch 'master' into fix/code
D-Sketon 8e54ce4
Merge branch 'master' into fix/code
D-Sketon 76a3c61
Merge branch 'master' into fix/code
D-Sketon 98d3bb9
fix(processor/code): enhance file exclusion logic in pattern matching
D-Sketon 7b6dd05
fix(tests): update code removal to use await for asynchronous operations
D-Sketon 0381f4c
Merge branch 'fix/code' of https://github.com/D-Sketon/hexo into fix/…
D-Sketon dc0b841
fix(processor/code): update slug assignment to use file path for bett…
D-Sketon 4c0af71
Merge branch 'master' into fix/code
D-Sketon bc05d0f
fix(processor): refactor code_dir handling for consistency in asset a…
D-Sketon 0687880
Merge branch 'fix/code' of https://github.com/D-Sketon/hexo into fix/…
D-Sketon e17b32e
fix(processor): ensure document modification status is updated correc…
D-Sketon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,20 @@ | ||
| import warehouse from 'warehouse'; | ||
| import type Hexo from '../hexo'; | ||
| import { CodeSchema } from '../types'; | ||
| import { join } from 'path'; | ||
|
|
||
| export = (ctx: Hexo) => { | ||
| const Code = new warehouse.Schema<CodeSchema>({ | ||
| _id: { type: String, required: true }, | ||
| path: { type: String, required: true }, | ||
| slug: { type: String, required: true }, | ||
| modified: { type: Boolean, default: true }, | ||
| content: { type: String, default: '' } | ||
| }); | ||
|
|
||
| Code.virtual('source').get(function() { | ||
| return join(ctx.base_dir, this._id); | ||
| }); | ||
|
|
||
| return Code; | ||
| }; |
This file contains hidden or 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 hidden or 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 hidden or 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,27 @@ | ||
| import type Hexo from '../../hexo'; | ||
| import Promise from 'bluebird'; | ||
| import { exists } from 'hexo-fs'; | ||
| import type { CodeSchema } from '../../types'; | ||
| import type Document from 'warehouse/dist/document'; | ||
|
|
||
| interface CodeData { | ||
| modified: boolean; | ||
| data: string; | ||
| } | ||
|
|
||
| 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 }; | ||
| }); | ||
|
|
||
| } | ||
|
|
||
| export = codeGenerator; |
This file contains hidden or 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 hidden or 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 hidden or 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,46 @@ | ||
| import { Pattern } from 'hexo-util'; | ||
| import { relative } from 'path'; | ||
| import { isExcludedFile } from './common'; | ||
| import type Hexo from '../../hexo'; | ||
| import type { _File } from '../../box'; | ||
|
|
||
| export = (ctx: Hexo) => { | ||
| return { | ||
| pattern: new Pattern(path => { | ||
| let codeDir = ctx.config.code_dir; | ||
| if (!codeDir.endsWith('/')) codeDir += '/'; | ||
| if (!path.startsWith(codeDir)) return false; | ||
| if (isExcludedFile(path, ctx.config)) return false; | ||
| return true; | ||
| }), | ||
| process: function codeProcessor(file: _File) { | ||
| const id = relative(ctx.base_dir, file.source).replace(/\\/g, '/'); | ||
| const slug = file.path; | ||
| const Code = ctx.model('Code'); | ||
| const doc = Code.findById(id); | ||
|
|
||
| if (file.type === 'delete') { | ||
| if (doc) { | ||
| return doc.remove(); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (file.type === 'skip' && doc) { | ||
| doc.modified = false; | ||
| return doc.save(); | ||
| } | ||
|
|
||
| return file.read().then(content => { | ||
| return Code.save({ | ||
| _id: id, | ||
| path: file.path, | ||
| slug, | ||
| modified: file.type !== 'skip', | ||
| content | ||
| }); | ||
| }); | ||
| } | ||
| }; | ||
| }; | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,116 @@ | ||
| import { join } from 'path'; | ||
| import { mkdirs, rmdir, unlink, writeFile } from 'hexo-fs'; | ||
| import Hexo from '../../../lib/hexo'; | ||
| import codeGenerator from '../../../lib/plugins/generator/code'; | ||
| import defaults from '../../../lib/hexo/default_config'; | ||
| import chai from 'chai'; | ||
| const should = chai.should(); | ||
| type CodeParams = Parameters<typeof codeGenerator> | ||
| type CodeReturn = ReturnType<typeof codeGenerator> | ||
|
|
||
| describe('code', () => { | ||
| const hexo = new Hexo(join(__dirname, 'code_test'), {silent: true}); | ||
| const generator: (...args: CodeParams) => CodeReturn = codeGenerator.bind(hexo); | ||
| const Code = hexo.model('Code'); | ||
| const codeDir = defaults.code_dir; | ||
|
|
||
| before(async () => { | ||
| await mkdirs(hexo.base_dir); | ||
| await hexo.init(); | ||
| }); | ||
|
|
||
| after(() => rmdir(hexo.base_dir)); | ||
|
|
||
| it('renderable', async () => { | ||
| const path = 'test.j2'; | ||
| const source = join(hexo.base_dir, defaults.source_dir, defaults.code_dir, path); | ||
| const content = '{{ 1 }}'; | ||
|
|
||
| await Promise.all([ | ||
| Code.insert({ | ||
| _id: `${defaults.source_dir}/${codeDir}/${path}`, | ||
| slug: `${codeDir}/${path}`, | ||
| path: `${codeDir}/${path}`, | ||
| content | ||
| }), | ||
| writeFile(source, content) | ||
| ]); | ||
| const data = await generator(); | ||
| const item = data.find(d => d.path === `${codeDir}/${path}`); | ||
| should.exist(item); | ||
| item.path.should.eql(`${codeDir}/${path}`); | ||
| item.data.modified.should.be.true; | ||
|
|
||
| const result = await item.data.data; | ||
| result.should.eql(content); | ||
|
|
||
| await Promise.all([ | ||
| Code.removeById(`${defaults.source_dir}/${codeDir}/${path}`), | ||
| unlink(source) | ||
| ]); | ||
| }); | ||
|
|
||
| it('not renderable', async () => { | ||
| const path = 'test.txt'; | ||
| const source = join(hexo.base_dir, defaults.source_dir, defaults.code_dir, path); | ||
| const content = 'test content'; | ||
|
|
||
| await Promise.all([ | ||
| Code.insert({ | ||
| _id: `${defaults.source_dir}/${codeDir}/${path}`, | ||
| slug: `${codeDir}/${path}`, | ||
| path: `${codeDir}/${path}`, | ||
| content | ||
| }), | ||
| writeFile(source, content) | ||
| ]); | ||
| const data = await generator(); | ||
| const item = data.find(d => d.path === `${codeDir}/${path}`); | ||
| should.exist(item); | ||
| item.path.should.eql(`${codeDir}/${path}`); | ||
| item.data.modified.should.be.true; | ||
|
|
||
| const result = await item.data.data; | ||
| result.should.eql(content); | ||
|
|
||
| await Promise.all([ | ||
| Code.removeById(`${defaults.source_dir}/${codeDir}/${path}`), | ||
| unlink(source) | ||
| ]); | ||
| }); | ||
|
|
||
| it('remove codes which does not exist', async () => { | ||
| const path = 'test.js'; | ||
|
|
||
| await Code.insert({ | ||
| _id: `${defaults.source_dir}/${codeDir}/${path}`, | ||
| slug: `${codeDir}/${path}`, | ||
| path: `${codeDir}/${path}` | ||
| }); | ||
| await generator(); | ||
| should.not.exist(Code.findById(`${defaults.source_dir}/${codeDir}/${path}`)); | ||
| }); | ||
|
|
||
| it('don\'t remove extension name', async () => { | ||
| const path = 'test.min.js'; | ||
| const source = join(hexo.base_dir, defaults.source_dir, defaults.code_dir, path); | ||
|
|
||
| await Promise.all([ | ||
| Code.insert({ | ||
| _id: `${defaults.source_dir}/${codeDir}/${path}`, | ||
| slug: `${codeDir}/${path}`, | ||
| path: `${codeDir}/${path}` | ||
| }), | ||
| writeFile(source, '') | ||
| ]); | ||
| const data = await generator(); | ||
| const item = data.find(d => d.path === `${codeDir}/${path}`); | ||
| should.exist(item); | ||
| item.path.should.eql(`${codeDir}/${path}`); | ||
|
|
||
| await Promise.all([ | ||
| Code.removeById(`${defaults.source_dir}/${codeDir}/${path}`), | ||
| unlink(source) | ||
| ]); | ||
| }); | ||
| }); |
This file contains hidden or 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,59 @@ | ||
| import { join } from 'path'; | ||
| import Hexo from '../../../lib/hexo'; | ||
|
|
||
| describe('Code', () => { | ||
| const hexo = new Hexo(); | ||
| const Code = hexo.model('Code'); | ||
|
|
||
| it('_id - required', async () => { | ||
| try { | ||
| await Code.insert({}); | ||
| } catch (err) { | ||
| err.message.should.eql('ID is not defined'); | ||
| } | ||
| }); | ||
|
|
||
| it('path - required', async () => { | ||
| try { | ||
| await Code.insert({ | ||
| _id: 'foo' | ||
| }); | ||
| } catch (err) { | ||
| err.message.should.eql('`path` is required!'); | ||
| } | ||
| }); | ||
|
|
||
| it('slug - required', async () => { | ||
| try { | ||
| await Code.insert({ | ||
| _id: 'foo', | ||
| path: 'bar' | ||
| }); | ||
| } catch (err) { | ||
| err.message.should.eql('`slug` is required!'); | ||
| } | ||
| }); | ||
|
|
||
| it('default values', async () => { | ||
| const data = await Code.insert({ | ||
| _id: 'foo', | ||
| path: 'bar', | ||
| slug: 'baz' | ||
| }); | ||
| data.modified.should.be.true; | ||
| data.content.should.eql(''); | ||
|
|
||
| Code.removeById(data._id); | ||
| }); | ||
|
|
||
| it('source - virtual', async () => { | ||
| const data = await Code.insert({ | ||
| _id: 'foo', | ||
| path: 'bar', | ||
| slug: 'baz' | ||
| }); | ||
| data.source.should.eql(join(hexo.base_dir, data._id)); | ||
|
|
||
| Code.removeById(data._id); | ||
| }); | ||
| }); |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.