Skip to content
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

some fixes as description #70

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 40 additions & 25 deletions lib/front_matter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,30 @@ function split(str: string) {
return { content: str };
}

function parse(str: string, options?: yaml.LoadOptions) {
export type ParseResult = Record<string, any> & 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!');

const splitData = split(str);
const raw = splitData.data;

if (!raw) return { _content: str };

let data;
let data: ParseResult;

if (splitData.separator.startsWith(';')) {
data = parseJSON(raw);
} else {
data = parseYAML(raw, options);
data = <any>parseYAML(raw, options);
dimaslanjaka marked this conversation as resolved.
Show resolved Hide resolved
}

if (!data) return { _content: str };
Expand All @@ -55,26 +65,28 @@ 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)
);
}
});

data._content = splitData.content;
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
}
}

Expand All @@ -93,12 +105,12 @@ function escapeYAML(str: string) {
}

interface Options {
mode?: 'json' | '',
prefixSeparator?: boolean,
separator?: string
mode?: 'json' | '';
prefixSeparator?: boolean;
separator?: string;
}
dimaslanjaka marked this conversation as resolved.
Show resolved Hide resolved

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

const { _content: content = '' } = obj;
Expand All @@ -123,12 +135,14 @@ function stringify(obj, options: Options = {}) {
return result;
}

function stringifyYAML(obj, options) {
type YamlMergedOpts = Options & yaml.DumpOptions;

function stringifyYAML(obj: Record<string, any>, 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];
Expand Down Expand Up @@ -162,24 +176,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 };
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -32,7 +32,7 @@
},
"devDependencies": {
"@types/js-yaml": "^4.0.5",
"@types/node": "^18.11.7",
"@types/node": "^18.16.3",
"@typescript-eslint/eslint-plugin": "^5.41.0",
"@typescript-eslint/parser": "^5.41.0",
"c8": "^7.12.0",
Expand Down
5 changes: 2 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
"outDir": "dist",
"declaration": true,
"esModuleInterop": true,
"types": [
"node"
]
"skipDefaultLibCheck": true,
"skipLibCheck": true
},
"include": [
"lib/front_matter.ts"
Expand Down