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

chore: 🤖 [WIP] init (remain ts errors) #16

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
103 changes: 103 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import detectNewline from "detect-newline";
import yaml from "js-yaml";

interface MetaData {
[key: string]: any;
}

/**
* @typedef {Object} ParseResult
* @property {string} content Markdown source text without metadata.
* @property {Object} metadata Plain object of parsed metadata.
*/
interface ParseResult {
content: string;
metadata: string | MetaData;
}

/**
* @param {string} text Markdown source text to parse.
* @returns {ParseResult} ParseResult object.
* @throws {TypeError} Markdown source text must be a string.
* @throws {YAMLException} YAML parser function error.
*/
const metadataParser = (text: string): ParseResult => {
let METADATA_START: RegExp;
let METADATA_END: string | any[];
let METADATA_FILE_END: string | any[];
let result: ParseResult = {
content: text,
metadata: "",
};

const validateMarkdownText = () => {
if (typeof text !== "string") {
throw new TypeError("Markdown source text must be a string.");
}
};

const setMetadataPatterns = () => {
const newline = detectNewline.graceful(text);
METADATA_START = new RegExp(`^---${newline}`);
METADATA_END = `${newline}---${newline}`;
METADATA_FILE_END = `${newline}---`;
};

const splitTextWithMetadata = () => {
const metadataEndIndex = text.indexOf(METADATA_END as string);
if (metadataEndIndex !== -1) {
result = {
content: text.substring(metadataEndIndex + METADATA_END.length),
metadata: text.substring(0, metadataEndIndex),
};
}
};

const splitTextWithOnlyMetadata = () => {
if (!result.metadata && text.endsWith(METADATA_FILE_END as string)) {
result = {
content: "",
metadata: text.substring(0, text.length - METADATA_FILE_END.length),
};
}
};

const extractContentAndMetadata = () => {
if (METADATA_START.test(text)) {
splitTextWithMetadata();
splitTextWithOnlyMetadata();
}
};

const removeStartPatternFromMetadata = () => {
result = {
...result,
metadata: result.metadata
? result.metadata!.replace(METADATA_START, "").trim()
: null,
};
};

const parseMetadata = () => {
const parseResult = result.metadata
? yaml.load(result.metadata as string)
: {};
result = {
...result,
metadata: parseResult as MetaData,
};
};

const parse = () => {
validateMarkdownText();
setMetadataPatterns();
extractContentAndMetadata();
removeStartPatternFromMetadata();
parseMetadata();
};

parse();
return result;
};

export default metadataParser;
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
},
"description": "Parse YAML metadata (front matter) in a markdown document",
"devDependencies": {
"@types/js-yaml": "^4.0.2",
"ascjs": "^4.0.3",
"c8": "^7.3.5",
"mkdirp": "^1.0.4",
"standard": "^16.0.3",
"tap": "^14.11.0"
"tap": "^14.11.0",
"typescript": "^4.3.5"
},
"engines": {
"node": ">=10.0.0"
Expand Down
21 changes: 21 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"target": "es2018",
"module": "commonjs",
"moduleResolution": "node",
"lib": ["es2018"],
"esModuleInterop": true,
"rootDir": "./lib",
"outDir": "./dist",
"sourceMap": true,
"declaration": true,
"declarationMap": true,
"strict": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"baseUrl": ".",
"types": ["@types/node"]
},
"exclude": ["node_modules", "dist", "**/*.spec.ts"],
"include": ["lib/**/*"]
}
Loading