Skip to content

Commit

Permalink
refactor: migrate ts
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Sketon committed Mar 3, 2024
1 parent 43f3c37 commit 1e77e44
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 42 deletions.
6 changes: 6 additions & 0 deletions test/fixtures/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "hexo/ts",
"rules": {
"node/no-unsupported-features/es-syntax": 0
}
}
16 changes: 7 additions & 9 deletions test/fixtures/post_render.js → test/fixtures/post_render.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
'use strict';

const { highlight } = require('hexo-util');
import { highlight } from 'hexo-util';

const code = [
'if tired && night:',
' sleep()'
].join('\n');

exports.content = [
export const content = [
'# Title',
'``` python',
code,
Expand All @@ -24,7 +22,7 @@ exports.content = [
'{% endquote %}'
].join('\n');

exports.expected = [
export const expected = [
'<h1 id="Title"><a href="#Title" class="headerlink" title="Title"></a>Title</h1>',
highlight(code, {lang: 'python'}),
'\n<p>some content</p>\n',
Expand All @@ -36,7 +34,7 @@ exports.expected = [
'<footer><strong>Hello World</strong></footer></blockquote>'
].join('');

exports.expected_disable_nunjucks = [
export const expected_disable_nunjucks = [
'<h1 id="Title"><a href="#Title" class="headerlink" title="Title"></a>Title</h1>',
highlight(code, {lang: 'python'}),
'\n<p>some content</p>\n',
Expand All @@ -49,7 +47,7 @@ exports.expected_disable_nunjucks = [
'{% endquote %}</p>'
].join('');

exports.content_for_issue_3346 = [
export const content_for_issue_3346 = [
'# Title',
'```',
'{% test1 %}',
Expand All @@ -63,7 +61,7 @@ exports.content_for_issue_3346 = [
'{% endblockquote %}'
].join('\n');

exports.expected_for_issue_3346 = [
export const expected_for_issue_3346 = [
'<h1 id="Title"><a href="#Title" class="headerlink" title="Title"></a>Title</h1>',
highlight('{% test1 %}\n{{ test2 }}').replace(/{/g, '&#123;').replace(/}/g, '&#125;'), // Escaped by backtick_code_block
'\n<p>some content</p>\n',
Expand All @@ -73,7 +71,7 @@ exports.expected_for_issue_3346 = [
'</blockquote>'
].join('');

exports.content_for_issue_4460 = [
export const content_for_issue_4460 = [
'```html',
'<body>',
'<!-- here goes the rest of the page -->',
Expand Down
4 changes: 2 additions & 2 deletions test/scripts/generators/asset.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { join } from 'path';
import { mkdirs, rmdir, unlink, writeFile } from 'hexo-fs';
import testUtil from '../../util';
import { readStream } from '../../util';
import Hexo from '../../../lib/hexo';
import assetGenerator from '../../../lib/plugins/generator/asset';
import { spy } from 'sinon';
Expand All @@ -15,7 +15,7 @@ describe('asset', () => {
const Asset = hexo.model('Asset');

const checkStream = async (stream, expected) => {
const data = await testUtil.stream.read(stream);
const data = await readStream(stream);
data.should.eql(expected);
};

Expand Down
4 changes: 2 additions & 2 deletions test/scripts/hexo/hexo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { mkdirs, rmdir, unlink, writeFile } from 'hexo-fs';
// @ts-ignore
import Promise from 'bluebird';
import { spy } from 'sinon';
import testUtil from '../../util';
import { readStream } from '../../util';
import { full_url_for } from 'hexo-util';
import Hexo from '../../../lib/hexo';
import chai from 'chai';
Expand All @@ -20,7 +20,7 @@ describe('Hexo', () => {
const { route } = hexo;

async function checkStream(stream, expected) {
const data = await testUtil.stream.read(stream);
const data = await readStream(stream);
data.should.eql(expected);
}

Expand Down
34 changes: 17 additions & 17 deletions test/scripts/hexo/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import moment from 'moment';
import { readFile, mkdirs, unlink, rmdir, writeFile, exists, stat, listDir } from 'hexo-fs';
import { spy, useFakeTimers } from 'sinon';
import { parse as yfm } from 'hexo-front-matter';
import fixture from '../../fixtures/post_render';
import { expected, content, expected_disable_nunjucks, content_for_issue_3346, expected_for_issue_3346, content_for_issue_4460 } from '../../fixtures/post_render';
import { highlight } from 'hexo-util';
import Hexo from '../../../lib/hexo';
import chai from 'chai';
Expand Down Expand Up @@ -659,17 +659,17 @@ describe('Post', () => {
hexo.extend.filter.register('after_post_render', afterHook);

const data = await post.render('', {
content: fixture.content,
content,
engine: 'markdown'
});
data.content.trim().should.eql(fixture.expected);
data.content.trim().should.eql(expected);
beforeHook.calledOnce.should.be.true;
afterHook.calledOnce.should.be.true;
});

it('render() - callback', done => {
post.render('', {
content: fixture.content,
content,
engine: 'markdown'
}, err => {
done(err);
Expand Down Expand Up @@ -795,10 +795,10 @@ describe('Post', () => {

try {
const data = await post.render('', {
content: fixture.content,
content,
engine: 'markdown'
});
data.content.trim().should.eql(fixture.expected_disable_nunjucks);
data.content.trim().should.eql(expected_disable_nunjucks);
} finally {
renderer.disableNunjucks = false;
}
Expand All @@ -811,10 +811,10 @@ describe('Post', () => {

try {
const data = await post.render('', {
content: fixture.content,
content,
engine: 'markdown'
});
data.content.trim().should.eql(fixture.expected);
data.content.trim().should.eql(expected);
} finally {
renderer.disableNunjucks = false;
}
Expand Down Expand Up @@ -848,11 +848,11 @@ describe('Post', () => {

try {
const data = await post.render('', {
content: fixture.content,
content,
engine: 'markdown',
disableNunjucks: false
});
data.content.trim().should.eql(fixture.expected);
data.content.trim().should.eql(expected);
} finally {
renderer.disableNunjucks = false;
}
Expand All @@ -864,11 +864,11 @@ describe('Post', () => {

try {
const data = await post.render('', {
content: fixture.content,
content,
engine: 'markdown',
disableNunjucks: true
});
data.content.trim().should.eql(fixture.expected_disable_nunjucks);
data.content.trim().should.eql(expected_disable_nunjucks);
} finally {
renderer.disableNunjucks = false;
}
Expand All @@ -881,12 +881,12 @@ describe('Post', () => {

try {
const data = await post.render('', {
content: fixture.content,
content,
engine: 'markdown',
// @ts-ignore
disableNunjucks: null
});
data.content.trim().should.eql(fixture.expected_disable_nunjucks);
data.content.trim().should.eql(expected_disable_nunjucks);
} finally {
renderer.disableNunjucks = false;
}
Expand Down Expand Up @@ -1155,14 +1155,14 @@ describe('Post', () => {

// #3346
it('render() - swig tag inside backtick code block', async () => {
const content = fixture.content_for_issue_3346;
const content = content_for_issue_3346;

const data = await post.render('', {
content,
engine: 'markdown'
});

data.content.trim().should.eql(fixture.expected_for_issue_3346);
data.content.trim().should.eql(expected_for_issue_3346);
});

// test for https://github.com/hexojs/hexo/pull/4171#issuecomment-594412367
Expand Down Expand Up @@ -1343,7 +1343,7 @@ describe('Post', () => {
it('render() - issue #4460', async () => {
hexo.config.syntax_highlighter = 'prismjs';

const content = fixture.content_for_issue_4460;
const content = content_for_issue_4460;

const data = await post.render('', {
content,
Expand Down
6 changes: 3 additions & 3 deletions test/scripts/hexo/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { join } from 'path';
import crypto from 'crypto';
import { createReadStream } from 'hexo-fs';
import { spy, assert as sinonAssert } from 'sinon';
import testUtil from '../../util';
import { readStream } from '../../util';
import Router from '../../../lib/hexo/router';
import chai from 'chai';
const should = chai.should();
Expand All @@ -14,7 +14,7 @@ describe('Router', () => {
const router = new Router();

function checkStream(stream, expected) {
return testUtil.stream.read(stream).then(data => {
return readStream(stream).then(data => {
data.should.eql(expected);
});
}
Expand Down Expand Up @@ -125,7 +125,7 @@ describe('Router', () => {
throw new Error('error test');
});

return testUtil.stream.read(router.get('test')).then(() => {
return readStream(router.get('test')).then(() => {
should.fail('Return value must be rejected');
}, err => {
err.should.have.property('message', 'error test');
Expand Down
6 changes: 6 additions & 0 deletions test/util/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "hexo/ts",
"rules": {
"node/no-unsupported-features/es-syntax": 0
}
}
3 changes: 0 additions & 3 deletions test/util/index.js

This file was deleted.

1 change: 1 addition & 0 deletions test/util/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { readStream } from './stream';
8 changes: 2 additions & 6 deletions test/util/stream.js → test/util/stream.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';
import Promise from 'bluebird';

const Promise = require('bluebird');

function readStream(stream) {
export function readStream(stream): Promise<string> {
return new Promise((resolve, reject) => {
let data = '';

Expand All @@ -13,5 +11,3 @@ function readStream(stream) {
}).on('error', reject);
});
}

exports.read = readStream;

0 comments on commit 1e77e44

Please sign in to comment.