Skip to content

Commit

Permalink
refactor: migrate typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Oct 28, 2022
1 parent 1adb3c6 commit 9028f91
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
coverage/
tmp/
tmp/
dist/
12 changes: 10 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
{
"extends": "hexo",
"root": true
"root": true,
"extends": "hexo/ts.js",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2020
},
"rules": {
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/ban-ts-comment": 0
}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ tmp/
*.log
.idea/
coverage
dist
package-lock.json
16 changes: 10 additions & 6 deletions lib/front_matter.js → lib/front_matter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

const yaml = require('js-yaml');
import yaml from 'js-yaml';
const rPrefixSep = /^(-{3,}|;{3,})/;
const rFrontMatter = /^(-{3,}|;{3,})\n([\s\S]+?)\n\1\n?([\s\S]*)/;
const rFrontMatterNew = /^([\s\S]+?)\n(-{3,}|;{3,})\n?([\s\S]*)/;
Expand Down Expand Up @@ -94,7 +92,13 @@ function escapeYAML(str) {
});
}

function stringify(obj, options = {}) {
interface Options {
mode?: 'json' | '',
prefixSeparator?: boolean,
separator?: string
}

function stringify(obj, options: Options = {}) {
if (!obj) throw new TypeError('obj is required!');

const { _content: content = '' } = obj;
Expand Down Expand Up @@ -173,9 +177,9 @@ function formatDate(date) {
return `${date.getFullYear()}-${doubleDigit(date.getMonth() + 1)}-${doubleDigit(date.getDate())} ${doubleDigit(date.getHours())}:${doubleDigit(date.getMinutes())}:${doubleDigit(date.getSeconds())}`;
}

module.exports = {
export {
parse,
split,
escape: escapeYAML,
escapeYAML as escape,
stringify
};
16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
"name": "hexo-front-matter",
"version": "4.0.0",
"description": "Front-matter parser.",
"main": "lib/front_matter",
"main": "dist/front_matter",
"scripts": {
"prepublish ": "npm run clean && npm run build",
"build": "tsc -b",
"clean": "tsc -b --clean",
"eslint": "eslint .",
"test": "mocha test/index.js",
"pretest": "npm run clean && npm run build",
"test": "mocha test/index.js --require ts-node/register",
"test-cov": "c8 --reporter=lcovonly npm run test"
},
"directories": {
Expand All @@ -29,11 +33,17 @@
"js-yaml": "^4.1.0"
},
"devDependencies": {
"@types/js-yaml": "^4.0.5",
"@types/node": "^18.11.7",
"@typescript-eslint/eslint-plugin": "^5.41.0",
"@typescript-eslint/parser": "^5.41.0",
"c8": "^7.12.0",
"chai": "^4.3.6",
"eslint": "^8.23.1",
"eslint-config-hexo": "^5.0.0",
"mocha": "^10.0.0"
"mocha": "^10.0.0",
"ts-node": "^10.9.1",
"typescript": "^4.8.4"
},
"engines": {
"node": ">=14"
Expand Down
6 changes: 5 additions & 1 deletion test/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"extends": "hexo/test"
"extends": "hexo/test",
"rules": {
"@typescript-eslint/no-var-requires": 0,
"@typescript-eslint/no-empty-function": 0
}
}
19 changes: 19 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"sourceMap": true,
"outDir": "dist",
"declaration": true,
"esModuleInterop": true,
"types": [
"node"
]
},
"include": [
"lib/front_matter.ts"
],
"exclude": [
"node_modules"
]
}

0 comments on commit 9028f91

Please sign in to comment.