Skip to content

Commit

Permalink
Implement own frontmatter parser (#17)
Browse files Browse the repository at this point in the history
Most open source solutions use js-yaml which is less preferred solution
these days and bloated with other deps.

Here added own frontmatter parser based on
https://github.com/vfile/vfile-matter
  • Loading branch information
TrySound committed Jun 7, 2023
1 parent 818e2cf commit 0c4a594
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 58 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/tests.yml
Expand Up @@ -30,11 +30,9 @@ jobs:
- name: Install dependencies
run: yarn --frozen-lockfile --prefer-offline

- name: Type check
run: yarn type-check
- run: yarn type-check

- name: End to end tests
run: yarn e2e
- run: yarn test
env:
GITHUB_INTROSPECTION_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_E2E_TESTS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5 changes: 0 additions & 5 deletions jest.config.js

This file was deleted.

18 changes: 14 additions & 4 deletions package.json
Expand Up @@ -12,8 +12,8 @@
"type-check": "tsc --noEmit",
"dev": "yarn codegen --watch",
"build": "rm -rf ./dist && yarn tsc --project tsconfig.build.json",
"e2e": "yarn codegen && yarn jest --testPathPattern=e2e",
"prepublishOnly": "yarn e2e",
"test": "yarn codegen && yarn jest",
"prepublishOnly": "yarn test",
"debug": "node -r dotenv-flow/config src/tmp/debug.js",
"docs": "typedoc src --readme none --githubPages false",
"prepare": "simple-git-hooks"
Expand All @@ -24,6 +24,16 @@
"nano-staged": {
"*.{ts,css,md}": "prettier --write"
},
"jest": {
"preset": "ts-jest",
"setupFiles": [
"dotenv-flow/config"
],
"testPathIgnorePatterns": [
"/node_modules/",
"/dist/"
]
},
"files": [
"package.json",
"README.md",
Expand All @@ -32,8 +42,8 @@
"dependencies": {
"classnames": "^2.3.1",
"code-tag": "^1.1.0",
"gray-matter": "^4.0.2",
"undici": "^5.22.1"
"undici": "^5.22.1",
"yaml": "^2.3.1"
},
"devDependencies": {
"@graphql-codegen/cli": "^4.0.1",
Expand Down
4 changes: 2 additions & 2 deletions src/datatypes/Post.ts
@@ -1,7 +1,7 @@
import matter from "gray-matter";
import { gql } from "code-tag";
import { createDataType } from "../core/datatype";
import { Post_IssueFragment } from "../types";
import { frontmatter } from "../utils/frontmatter";

import { Reactions } from "./Reactions";
import { Labels } from "./Labels";
Expand Down Expand Up @@ -50,7 +50,7 @@ export const Post = createDataType<PostInput, Post>({
}
`,
translator: (issue) => {
const { data, content } = matter(issue.body);
const { data, content } = frontmatter(issue.body);

return {
id: issue.id,
Expand Down
42 changes: 42 additions & 0 deletions src/utils/frontmatter.test.ts
@@ -0,0 +1,42 @@
import { frontmatter } from "./frontmatter";

const someYaml = "---\nkey: value\nlist:\n - 1\n - 2\n---";
const someData = { key: "value", list: [1, 2] };
const doc = "Here is a document\nMore of the document\nOther lines\n";
const both = someYaml + "\n" + doc;

test("should parse and strip frontmatter", () => {
expect(frontmatter(both)).toEqual({
data: someData,
content: doc,
});
});

test("should support no matter", () => {
expect(frontmatter(doc)).toEqual({
data: {},
content: doc,
});
});

test("should strip matter completely", () => {
expect(frontmatter(someYaml)).toEqual({
data: someData,
content: "",
});
});

test("should handle thematic breaks", () => {
const extra = "Here is a thematic break\n---\nEnd";
expect(frontmatter(both + extra)).toEqual({
data: someData,
content: doc + extra,
});
});

test("should support additional newline before closing matter", () => {
expect(frontmatter("---\nkey: value\n\n---\n" + doc)).toEqual({
data: { key: "value" },
content: doc,
});
});
17 changes: 17 additions & 0 deletions src/utils/frontmatter.ts
@@ -0,0 +1,17 @@
// inspired by https://github.com/vfile/vfile-matter

import { parse } from "yaml";

export const frontmatter = (content: string) => {
const match = /^---(?:\r?\n|\r)(?:([\s\S]*?)(?:\r?\n|\r))?---(?:\r?\n|\r|$)/.exec(content);
if (match) {
return {
data: parse(match[1]),
content: content.slice(match[0].length),
};
}
return {
data: {},
content,
};
};
3 changes: 1 addition & 2 deletions tsconfig.json
Expand Up @@ -10,7 +10,6 @@
"esModuleInterop": true,
"resolveJsonModule": true,
"isolatedModules": true,
"outDir": "./dist",
"baseUrl": "./src"
"outDir": "./dist"
}
}
47 changes: 6 additions & 41 deletions yarn.lock
Expand Up @@ -2765,13 +2765,6 @@ expect@^29.0.0, expect@^29.5.0:
jest-message-util "^29.5.0"
jest-util "^29.5.0"

extend-shallow@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/extend-shallow/-/extend-shallow-2.0.1.tgz#51af7d614ad9a9f610ea1bafbb989d6b1c56890f"
integrity sha1-Ua99YUrZqfYQ6huvu5idaxxWiQ8=
dependencies:
is-extendable "^0.1.0"

external-editor@^3.0.3:
version "3.1.0"
resolved "https://registry.yarnpkg.com/external-editor/-/external-editor-3.1.0.tgz#cb03f740befae03ea4d283caed2741a83f335495"
Expand Down Expand Up @@ -3073,16 +3066,6 @@ graphql@^15.5.0:
resolved "https://registry.yarnpkg.com/graphql/-/graphql-15.5.0.tgz#39d19494dbe69d1ea719915b578bf920344a69d5"
integrity sha512-OmaM7y0kaK31NKG31q4YbD2beNYa6jBBKtMFT6gLYJljHLJr42IqJ8KX08u3Li/0ifzTU5HjmoOOrwa5BRLeDA==

gray-matter@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/gray-matter/-/gray-matter-4.0.2.tgz#9aa379e3acaf421193fce7d2a28cebd4518ac454"
integrity sha512-7hB/+LxrOjq/dd8APlK0r24uL/67w7SkYnfwhNFwg/VDIGWGmduTDYf3WNstLW2fbbmRwrDGCVSJ2isuf2+4Hw==
dependencies:
js-yaml "^3.11.0"
kind-of "^6.0.2"
section-matter "^1.0.0"
strip-bom-string "^1.0.0"

handlebars@^4.7.7:
version "4.7.7"
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.7.7.tgz#9ce33416aad02dbd6c8fafa8240d5d98004945a1"
Expand Down Expand Up @@ -3277,11 +3260,6 @@ is-core-module@^2.2.0:
dependencies:
has "^1.0.3"

is-extendable@^0.1.0:
version "0.1.1"
resolved "https://registry.yarnpkg.com/is-extendable/-/is-extendable-0.1.1.tgz#62b110e289a471418e3ec36a617d472e301dfc89"
integrity sha1-YrEQ4omkcUGOPsNqYX1HLjAd/Ik=

is-extglob@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/is-extglob/-/is-extglob-2.1.1.tgz#a88c02535791f02ed37c76a1b9ea9773c833f8c2"
Expand Down Expand Up @@ -3807,7 +3785,7 @@ jose@^4.11.4:
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==

js-yaml@^3.11.0, js-yaml@^3.13.1:
js-yaml@^3.13.1:
version "3.14.1"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.14.1.tgz#dae812fdb3825fa306609a8717383c50c36a0537"
integrity sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==
Expand Down Expand Up @@ -3884,11 +3862,6 @@ jsonify@~0.0.0:
resolved "https://registry.yarnpkg.com/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73"
integrity sha1-LHS27kHZPKUbe1qu6PUDYx0lKnM=

kind-of@^6.0.0, kind-of@^6.0.2:
version "6.0.3"
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-6.0.3.tgz#07c05034a6c349fa06e24fa35aa76db4580ce4dd"
integrity sha512-dcS1ul+9tmeD95T+x28/ehLgd9mENa3LsvDTtzm3vyBEO7RPptvAD+t44WVXaUjTBRcrpFeFlC8WCruUR456hw==

kleur@^3.0.3:
version "3.0.3"
resolved "https://registry.yarnpkg.com/kleur/-/kleur-3.0.3.tgz#a79c9ecc86ee1ce3fa6206d1216c501f147fc07e"
Expand Down Expand Up @@ -4649,14 +4622,6 @@ scuid@^1.1.0:
resolved "https://registry.yarnpkg.com/scuid/-/scuid-1.1.0.tgz#d3f9f920956e737a60f72d0e4ad280bf324d5dab"
integrity sha512-MuCAyrGZcTLfQoH2XoBlQ8C6bzwN88XT/0slOGz0pn8+gIP85BOAfYa44ZXQUTOwRwPU0QvgU+V+OSajl/59Xg==

section-matter@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/section-matter/-/section-matter-1.0.0.tgz#e9041953506780ec01d59f292a19c7b850b84167"
integrity sha512-vfD3pmTzGpufjScBh50YHKzEu2lxBWhVEHsNGoEXmCmn2hKGfeNLYMzCJpe8cD7gqX7TJluOVpBkAequ6dgMmA==
dependencies:
extend-shallow "^2.0.1"
kind-of "^6.0.0"

semver@7.x, semver@^7.2.1:
version "7.3.5"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.5.tgz#0b621c879348d8998e4b0e4be94b3f12e6018ef7"
Expand Down Expand Up @@ -4871,11 +4836,6 @@ strip-ansi@^6.0.1:
dependencies:
ansi-regex "^5.0.1"

strip-bom-string@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/strip-bom-string/-/strip-bom-string-1.0.0.tgz#e5211e9224369fbb81d633a2f00044dc8cedad92"
integrity sha1-5SEekiQ2n7uB1jOi8ABE3IztrZI=

strip-bom@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-4.0.0.tgz#9c3505c1db45bcedca3d9cf7a16f5c5aa3901878"
Expand Down Expand Up @@ -5301,6 +5261,11 @@ yaml@^1.10.0:
resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.2.tgz#2301c5ffbf12b467de8da2333a459e29e7920e4b"
integrity sha512-r3vXyErRCYJ7wg28yvBY5VSoAF8ZvlcW9/BwUzEtUsjvX/DKs24dIkuwjtuprwJJHsbyUbLApepYTR1BN4uHrg==

yaml@^2.3.1:
version "2.3.1"
resolved "https://registry.yarnpkg.com/yaml/-/yaml-2.3.1.tgz#02fe0975d23cd441242aa7204e09fc28ac2ac33b"
integrity sha512-2eHWfjaoXgTBC2jNM1LRef62VQa0umtvRiDSk6HSzW7RvS5YtkabJrwYLLEKWBc8a5U2PTSCs+dJjUTJdlHsWQ==

yargs-parser@^18.1.2:
version "18.1.3"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-18.1.3.tgz#be68c4975c6b2abf469236b0c870362fab09a7b0"
Expand Down

0 comments on commit 0c4a594

Please sign in to comment.