From ac12477d2d76dff83c0c7b85215cd4d75f1aa96e Mon Sep 17 00:00:00 2001 From: dimaslanjaka Date: Sat, 22 Apr 2023 17:27:58 +0700 Subject: [PATCH 01/17] feat: make options argument optional --- lib/front_matter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/front_matter.ts b/lib/front_matter.ts index fbd3bd5..9da7101 100644 --- a/lib/front_matter.ts +++ b/lib/front_matter.ts @@ -32,7 +32,7 @@ function split(str: string) { return { content: str }; } -function parse(str: string, options: yaml.LoadOptions) { +function parse(str: string, options?: yaml.LoadOptions) { if (typeof str !== 'string') throw new TypeError('str is required!'); const splitData = split(str); From 2f1c469a83c44c9cfd91d994d7c5644b428004b2 Mon Sep 17 00:00:00 2001 From: dimaslanjaka Date: Sat, 22 Apr 2023 17:39:29 +0700 Subject: [PATCH 02/17] fix(deps): using `@types/node` instead `types` opt --- package.json | 2 +- tsconfig.json | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index d55bffd..8563556 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ }, "devDependencies": { "@types/js-yaml": "^4.0.5", - "@types/node": "^18.11.7", + "@types/node": "^18.15.13", "@typescript-eslint/eslint-plugin": "^5.41.0", "@typescript-eslint/parser": "^5.41.0", "c8": "^7.12.0", diff --git a/tsconfig.json b/tsconfig.json index c2b1773..749e33e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,10 +5,7 @@ "sourceMap": true, "outDir": "dist", "declaration": true, - "esModuleInterop": true, - "types": [ - "node" - ] + "esModuleInterop": true }, "include": [ "lib/front_matter.ts" From 0bed271c71a08bb6117aae02485f228cf44d27c8 Mon Sep 17 00:00:00 2001 From: dimaslanjaka Date: Sat, 22 Apr 2023 17:49:43 +0700 Subject: [PATCH 03/17] chore: arg options is empty object as default --- lib/front_matter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/front_matter.ts b/lib/front_matter.ts index 9da7101..7f364de 100644 --- a/lib/front_matter.ts +++ b/lib/front_matter.ts @@ -32,7 +32,7 @@ function split(str: string) { return { content: str }; } -function parse(str: string, options?: yaml.LoadOptions) { +function parse(str: string, options: yaml.LoadOptions = {}) { if (typeof str !== 'string') throw new TypeError('str is required!'); const splitData = split(str); From bd5a76e82057303f54a4c285dd03c1eb86aa11ae Mon Sep 17 00:00:00 2001 From: dimaslanjaka Date: Sun, 23 Apr 2023 17:37:35 +0700 Subject: [PATCH 04/17] chore: re-order object key --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 8563556..6ca1ffb 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,8 @@ "name": "hexo-front-matter", "version": "4.1.0", "description": "Front-matter parser.", - "main": "dist/front_matter", + "main": "dist/front_matter.js", + "types": "./dist/front_matter.d.ts", "scripts": { "prepublish ": "npm run clean && npm run build", "build": "tsc -b", @@ -13,9 +14,8 @@ "test-cov": "c8 --reporter=lcovonly npm run test" }, "files": [ - "dist/**" + "dist" ], - "types": "./dist/front_matter.d.ts", "repository": "hexojs/hexo-front-matter", "keywords": [ "front-matter", From 25b35eff3e9c2960cadbade7c0795b3b8bcc859c Mon Sep 17 00:00:00 2001 From: dimaslanjaka Date: Sun, 23 Apr 2023 17:43:55 +0700 Subject: [PATCH 05/17] chore: fix cast types - add data types - add parameter types --- lib/front_matter.ts | 61 +++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/lib/front_matter.ts b/lib/front_matter.ts index 7f364de..a57fc5c 100644 --- a/lib/front_matter.ts +++ b/lib/front_matter.ts @@ -40,12 +40,20 @@ function parse(str: string, options: yaml.LoadOptions = {}) { if (!raw) return { _content: str }; - let data; + let data: Partial<{ + [key: string]: any; + _content: string; + title: string; + description: string; + thumbnail: string; + date: any; + updated: any; + }>; if (splitData.separator.startsWith(';')) { data = parseJSON(raw); } else { - data = parseYAML(raw, options); + data = parseYAML(raw, options); } if (!data) return { _content: str }; @@ -55,7 +63,9 @@ function parse(str: string, options: yaml.LoadOptions = {}) { const item = data[key]; if (item instanceof Date) { - data[key] = new Date(item.getTime() + (item.getTimezoneOffset() * 60 * 1000)); + data[key] = new Date( + item.getTime() + (item.getTimezoneOffset() * 60 * 1000) + ); } }); @@ -63,18 +73,18 @@ function parse(str: string, options: yaml.LoadOptions = {}) { return data; } -function parseYAML(str, options: yaml.LoadOptions) { +function parseYAML(str: string, options: yaml.LoadOptions) { const result = yaml.load(escapeYAML(str), options); if (typeof result !== 'object') return; return result; } -function parseJSON(str) { +function parseJSON(str: string) { try { return JSON.parse(`{${str}}`); } catch (err) { - return; // eslint-disable-line + return; // eslint-disable-line } } @@ -93,12 +103,12 @@ function escapeYAML(str: string) { } interface Options { - mode?: 'json' | '', - prefixSeparator?: boolean, - separator?: string + mode?: 'json' | ''; + prefixSeparator?: boolean; + separator?: string; } -function stringify(obj, options: Options = {}) { +function stringify(obj: Record, options: Options = {}) { if (!obj) throw new TypeError('obj is required!'); const { _content: content = '' } = obj; @@ -123,12 +133,14 @@ function stringify(obj, options: Options = {}) { return result; } -function stringifyYAML(obj, options) { +type YamlMergedOpts = Options & yaml.DumpOptions; + +function stringifyYAML(obj: Record, options: YamlMergedOpts) { const keys = Object.keys(obj); const data = {}; const nullKeys = []; const dateKeys = []; - let key, value, i, len; + let key: string, value: any, i: number, len: number; for (i = 0, len = keys.length; i < len; i++) { key = keys[i]; @@ -162,24 +174,25 @@ function stringifyYAML(obj, options) { } function stringifyJSON(obj) { - return JSON.stringify(obj, null, ' ') + return ( + JSON.stringify(obj, null, ' ') // Remove indention - .replace(/\n {2}/g, () => '\n') + .replace(/\n {2}/g, () => '\n') // Remove prefixing and trailing braces - .replace(/^{\n|}$/g, ''); + .replace(/^{\n|}$/g, '') + ); } -function doubleDigit(num) { +function doubleDigit(num: number) { return num.toString().padStart(2, '0'); } -function formatDate(date) { - return `${date.getFullYear()}-${doubleDigit(date.getMonth() + 1)}-${doubleDigit(date.getDate())} ${doubleDigit(date.getHours())}:${doubleDigit(date.getMinutes())}:${doubleDigit(date.getSeconds())}`; +function formatDate(date: Date) { + return `${date.getFullYear()}-${doubleDigit( + date.getMonth() + 1 + )}-${doubleDigit(date.getDate())} ${doubleDigit( + date.getHours() + )}:${doubleDigit(date.getMinutes())}:${doubleDigit(date.getSeconds())}`; } -export { - parse, - split, - escapeYAML as escape, - stringify -}; +export { parse, split, escapeYAML as escape, stringify }; From 493fd35100f350d7239f8080ff698693863498a5 Mon Sep 17 00:00:00 2001 From: dimaslanjaka Date: Sun, 23 Apr 2023 17:53:59 +0700 Subject: [PATCH 06/17] chore: separate type result --- lib/front_matter.ts | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/front_matter.ts b/lib/front_matter.ts index a57fc5c..5f7015b 100644 --- a/lib/front_matter.ts +++ b/lib/front_matter.ts @@ -32,6 +32,15 @@ function split(str: string) { return { content: str }; } +export type ParseResult = Record & Partial<{ + _content: string; + title: string; + description: string; + thumbnail: string; + date: any; + updated: any; +}> + function parse(str: string, options: yaml.LoadOptions = {}) { if (typeof str !== 'string') throw new TypeError('str is required!'); @@ -40,15 +49,7 @@ function parse(str: string, options: yaml.LoadOptions = {}) { if (!raw) return { _content: str }; - let data: Partial<{ - [key: string]: any; - _content: string; - title: string; - description: string; - thumbnail: string; - date: any; - updated: any; - }>; + let data: ParseResult; if (splitData.separator.startsWith(';')) { data = parseJSON(raw); From 594844a55b7261244398648939a6e19102e64210 Mon Sep 17 00:00:00 2001 From: dimaslanjaka Date: Sun, 23 Apr 2023 17:54:55 +0700 Subject: [PATCH 07/17] feat: add `permalink` to return type --- lib/front_matter.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/front_matter.ts b/lib/front_matter.ts index 5f7015b..582fd30 100644 --- a/lib/front_matter.ts +++ b/lib/front_matter.ts @@ -39,6 +39,7 @@ export type ParseResult = Record & Partial<{ thumbnail: string; date: any; updated: any; + permalink: string; }> function parse(str: string, options: yaml.LoadOptions = {}) { From 68b5eef00b2c593378a7d7f4d8f0695b71726b2b Mon Sep 17 00:00:00 2001 From: dimaslanjaka Date: Tue, 2 May 2023 17:16:38 +0700 Subject: [PATCH 08/17] chore(deps): add `@types/hexo` fix: error TS2503: Cannot find namespace 'fs'. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6ca1ffb..a8394bb 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ }, "devDependencies": { "@types/js-yaml": "^4.0.5", - "@types/node": "^18.15.13", + "@types/node": "^18.16.3", "@typescript-eslint/eslint-plugin": "^5.41.0", "@typescript-eslint/parser": "^5.41.0", "c8": "^7.12.0", From 03fc40d272367f83ce9b0234288735c40a98739c Mon Sep 17 00:00:00 2001 From: dimaslanjaka Date: Tue, 2 May 2023 17:17:37 +0700 Subject: [PATCH 09/17] fix: failed build caused by libs in node_modules node_modules/@types/hexo/dist/hexo/index.d.ts:4:22 - error TS2307: Cannot find module 'warehouse' or its corresponding type declarations. --- tsconfig.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index 749e33e..476182c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,9 @@ "sourceMap": true, "outDir": "dist", "declaration": true, - "esModuleInterop": true + "esModuleInterop": true, + "skipDefaultLibCheck": true, + "skipLibCheck": true }, "include": [ "lib/front_matter.ts" From 16b8c62374c63b741767a900b5ec5fb55eb57dd4 Mon Sep 17 00:00:00 2001 From: Dimas Lanjaka Date: Sat, 6 May 2023 14:07:42 +0700 Subject: [PATCH 10/17] Update lib/front_matter.ts Co-authored-by: Sukka --- lib/front_matter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/front_matter.ts b/lib/front_matter.ts index 582fd30..624bbb8 100644 --- a/lib/front_matter.ts +++ b/lib/front_matter.ts @@ -55,7 +55,7 @@ function parse(str: string, options: yaml.LoadOptions = {}) { if (splitData.separator.startsWith(';')) { data = parseJSON(raw); } else { - data = parseYAML(raw, options); + data = parseYAML(raw, options) as any; } if (!data) return { _content: str }; From ef9668e66c4affeb085b4ec5076515c6b2a4815a Mon Sep 17 00:00:00 2001 From: dimaslanjaka Date: Sat, 6 May 2023 14:13:48 +0700 Subject: [PATCH 11/17] refactor: ignore dist prevent VSCode indexing `dist` while developing --- tsconfig.json | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/tsconfig.json b/tsconfig.json index 476182c..91d1c90 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,18 +1,14 @@ { - "compilerOptions": { - "module": "commonjs", - "target": "es6", - "sourceMap": true, - "outDir": "dist", - "declaration": true, - "esModuleInterop": true, - "skipDefaultLibCheck": true, - "skipLibCheck": true - }, - "include": [ - "lib/front_matter.ts" - ], - "exclude": [ - "node_modules" - ] -} \ No newline at end of file + "compilerOptions": { + "module": "CommonJS", + "target": "ES6", + "sourceMap": true, + "outDir": "dist", + "declaration": true, + "esModuleInterop": true, + "skipDefaultLibCheck": true, + "skipLibCheck": true + }, + "include": ["lib/front_matter.ts"], + "exclude": ["node_modules", "dist"] +} From 5d35fe8507f8727db849f1cde60a0b893e716eb8 Mon Sep 17 00:00:00 2001 From: dimaslanjaka Date: Sat, 6 May 2023 14:15:04 +0700 Subject: [PATCH 12/17] chore(lint): eslint --fix --- lib/front_matter.ts | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/front_matter.ts b/lib/front_matter.ts index 624bbb8..1f20d4b 100644 --- a/lib/front_matter.ts +++ b/lib/front_matter.ts @@ -32,15 +32,16 @@ function split(str: string) { return { content: str }; } -export type ParseResult = Record & Partial<{ - _content: string; - title: string; - description: string; - thumbnail: string; - date: any; - updated: any; - permalink: string; -}> +export type ParseResult = Record & + Partial<{ + _content: string; + title: string; + description: string; + thumbnail: string; + date: any; + updated: any; + permalink: string; + }>; function parse(str: string, options: yaml.LoadOptions = {}) { if (typeof str !== 'string') throw new TypeError('str is required!'); From bb5503f43eff62e670bed8029a13d18f14114ba5 Mon Sep 17 00:00:00 2001 From: dimaslanjaka Date: Sat, 6 May 2023 14:17:56 +0700 Subject: [PATCH 13/17] refactor: ignore .vscode settings --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index a611977..a4f510f 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ tmp/ .idea/ coverage dist -package-lock.json \ No newline at end of file +package-lock.json +.vscode \ No newline at end of file From 6ec68d7d737dd12f3441b7093af745e0b416677c Mon Sep 17 00:00:00 2001 From: dimaslanjaka Date: Sat, 6 May 2023 14:22:03 +0700 Subject: [PATCH 14/17] chore(deps-dev): add `husky` and `lint-staged` to automatically `eslint --fix` before commited --- .husky/pre-commit | 4 ++++ package.json | 5 ++++- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 .husky/pre-commit diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100644 index 0000000..36af219 --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1,4 @@ +#!/bin/sh +. "$(dirname "$0")/_/husky.sh" + +npx lint-staged diff --git a/package.json b/package.json index a8394bb..edc3c96 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "eslint": "eslint .", "pretest": "npm run clean && npm run build", "test": "mocha test/index.js --require ts-node/register", - "test-cov": "c8 --reporter=lcovonly npm run test" + "test-cov": "c8 --reporter=lcovonly npm run test", + "prepare": "husky install" }, "files": [ "dist" @@ -39,6 +40,8 @@ "chai": "^4.3.6", "eslint": "^8.23.1", "eslint-config-hexo": "^5.0.0", + "husky": "^8.0.3", + "lint-staged": "^13.2.2", "mocha": "^10.0.0", "ts-node": "^10.9.1", "typescript": "^4.8.4" From 87dd66e6ce413194f04b7c4c1bd7b9a204a7ddbf Mon Sep 17 00:00:00 2001 From: dimaslanjaka Date: Sat, 6 May 2023 14:22:20 +0700 Subject: [PATCH 15/17] feat: add `lint-staged` configuration --- lint-staged.config.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 lint-staged.config.js diff --git a/lint-staged.config.js b/lint-staged.config.js new file mode 100644 index 0000000..c873409 --- /dev/null +++ b/lint-staged.config.js @@ -0,0 +1,14 @@ +'use strict'; + +/** + * @type {{ [key: string]: import("lint-staged").Commands | import("lint-staged").ConfigFn }} + */ +const config = { + '*.ts': ['npx prettier --write', 'eslint --fix'], + '*.html': ['npx eslint --fix', 'prettier --write'], + '*.scss': 'npx prettier --write', + '*.json': 'npx prettier --write', + '*.yml': 'npx prettier --write' +}; + +module.exports = config; From c503f8389b0e5b2440d31c040ba929a42ec7881c Mon Sep 17 00:00:00 2001 From: dimaslanjaka Date: Sat, 6 May 2023 14:35:01 +0700 Subject: [PATCH 16/17] chore(stringify): return boundary as `string` --- lib/front_matter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/front_matter.ts b/lib/front_matter.ts index 1f20d4b..dc78d73 100644 --- a/lib/front_matter.ts +++ b/lib/front_matter.ts @@ -111,7 +111,7 @@ interface Options { separator?: string; } -function stringify(obj: Record, options: Options = {}) { +function stringify(obj: Record, options: Options = {}): string { if (!obj) throw new TypeError('obj is required!'); const { _content: content = '' } = obj; From 5c2e8c5c3fa1712c0ddddc71c9b75182939e34bf Mon Sep 17 00:00:00 2001 From: dimaslanjaka Date: Sat, 6 May 2023 14:35:48 +0700 Subject: [PATCH 17/17] refactor: only lint eslint for `ts`,`js` files drop `prettier` --- lint-staged.config.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lint-staged.config.js b/lint-staged.config.js index c873409..dcd2649 100644 --- a/lint-staged.config.js +++ b/lint-staged.config.js @@ -1,12 +1,9 @@ -'use strict'; - /** * @type {{ [key: string]: import("lint-staged").Commands | import("lint-staged").ConfigFn }} */ const config = { - '*.ts': ['npx prettier --write', 'eslint --fix'], - '*.html': ['npx eslint --fix', 'prettier --write'], - '*.scss': 'npx prettier --write', + '*.ts': 'eslint --fix', + '*.js': 'eslint --fix', '*.json': 'npx prettier --write', '*.yml': 'npx prettier --write' };