Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0c4a594

Browse files
authored
Implement own frontmatter parser (#17)
Most open source solutions use js-yaml which is less preferred solutionthese days and bloated with other deps.Here added own frontmatter parser based onhttps://github.com/vfile/vfile-matter
1 parent818e2cf commit0c4a594

File tree

8 files changed

+84
-58
lines changed

8 files changed

+84
-58
lines changed

‎.github/workflows/tests.yml‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@ jobs:
3030
-name:Install dependencies
3131
run:yarn --frozen-lockfile --prefer-offline
3232

33-
-name:Type check
34-
run:yarn type-check
33+
-run:yarn type-check
3534

36-
-name:End to end tests
37-
run:yarn e2e
35+
-run:yarn test
3836
env:
3937
GITHUB_INTROSPECTION_TOKEN:${{ secrets.GITHUB_TOKEN }}
4038
GITHUB_E2E_TESTS_TOKEN:${{ secrets.GITHUB_TOKEN }}

‎jest.config.js‎

Lines changed: 0 additions & 5 deletions
This file was deleted.

‎package.json‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
"type-check":"tsc --noEmit",
1313
"dev":"yarn codegen --watch",
1414
"build":"rm -rf ./dist && yarn tsc --project tsconfig.build.json",
15-
"e2e":"yarn codegen && yarn jest --testPathPattern=e2e",
16-
"prepublishOnly":"yarne2e",
15+
"test":"yarn codegen && yarn jest",
16+
"prepublishOnly":"yarntest",
1717
"debug":"node -r dotenv-flow/config src/tmp/debug.js",
1818
"docs":"typedoc src --readme none --githubPages false",
1919
"prepare":"simple-git-hooks"
@@ -24,6 +24,16 @@
2424
"nano-staged": {
2525
"*.{ts,css,md}":"prettier --write"
2626
},
27+
"jest": {
28+
"preset":"ts-jest",
29+
"setupFiles": [
30+
"dotenv-flow/config"
31+
],
32+
"testPathIgnorePatterns": [
33+
"/node_modules/",
34+
"/dist/"
35+
]
36+
},
2737
"files": [
2838
"package.json",
2939
"README.md",
@@ -32,8 +42,8 @@
3242
"dependencies": {
3343
"classnames":"^2.3.1",
3444
"code-tag":"^1.1.0",
35-
"gray-matter":"^4.0.2",
36-
"undici":"^5.22.1"
45+
"undici":"^5.22.1",
46+
"yaml":"^2.3.1"
3747
},
3848
"devDependencies": {
3949
"@graphql-codegen/cli":"^4.0.1",

‎src/datatypes/Post.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
importmatterfrom"gray-matter";
21
import{gql}from"code-tag";
32
import{createDataType}from"../core/datatype";
43
import{Post_IssueFragment}from"../types";
4+
import{frontmatter}from"../utils/frontmatter";
55

66
import{Reactions}from"./Reactions";
77
import{Labels}from"./Labels";
@@ -50,7 +50,7 @@ export const Post = createDataType<PostInput, Post>({
5050
}
5151
`,
5252
translator:(issue)=>{
53-
const{ data, content}=matter(issue.body);
53+
const{ data, content}=frontmatter(issue.body);
5454

5555
return{
5656
id:issue.id,

‎src/utils/frontmatter.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import{frontmatter}from"./frontmatter";
2+
3+
constsomeYaml="---\nkey: value\nlist:\n - 1\n - 2\n---";
4+
constsomeData={key:"value",list:[1,2]};
5+
constdoc="Here is a document\nMore of the document\nOther lines\n";
6+
constboth=someYaml+"\n"+doc;
7+
8+
test("should parse and strip frontmatter",()=>{
9+
expect(frontmatter(both)).toEqual({
10+
data:someData,
11+
content:doc,
12+
});
13+
});
14+
15+
test("should support no matter",()=>{
16+
expect(frontmatter(doc)).toEqual({
17+
data:{},
18+
content:doc,
19+
});
20+
});
21+
22+
test("should strip matter completely",()=>{
23+
expect(frontmatter(someYaml)).toEqual({
24+
data:someData,
25+
content:"",
26+
});
27+
});
28+
29+
test("should handle thematic breaks",()=>{
30+
constextra="Here is a thematic break\n---\nEnd";
31+
expect(frontmatter(both+extra)).toEqual({
32+
data:someData,
33+
content:doc+extra,
34+
});
35+
});
36+
37+
test("should support additional newline before closing matter",()=>{
38+
expect(frontmatter("---\nkey: value\n\n---\n"+doc)).toEqual({
39+
data:{key:"value"},
40+
content:doc,
41+
});
42+
});

‎src/utils/frontmatter.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// inspired by https://github.com/vfile/vfile-matter
2+
3+
import{parse}from"yaml";
4+
5+
exportconstfrontmatter=(content:string)=>{
6+
constmatch=/^---(?:\r?\n|\r)(?:([\s\S]*?)(?:\r?\n|\r))?---(?:\r?\n|\r|$)/.exec(content);
7+
if(match){
8+
return{
9+
data:parse(match[1]),
10+
content:content.slice(match[0].length),
11+
};
12+
}
13+
return{
14+
data:{},
15+
content,
16+
};
17+
};

‎tsconfig.json‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
"esModuleInterop":true,
1111
"resolveJsonModule":true,
1212
"isolatedModules":true,
13-
"outDir":"./dist",
14-
"baseUrl":"./src"
13+
"outDir":"./dist"
1514
}
1615
}

‎yarn.lock‎

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2765,13 +2765,6 @@ expect@^29.0.0, expect@^29.5.0:
27652765
jest-message-util "^29.5.0"
27662766
jest-util "^29.5.0"
27672767

2768-
extend-shallow@^2.0.1:
2769-
version "2.0.1"
2770-
resolved "https://registry.yarnpkg.com/extend-shallow/-/extend-shallow-2.0.1.tgz#51af7d614ad9a9f610ea1bafbb989d6b1c56890f"
2771-
integrity sha1-Ua99YUrZqfYQ6huvu5idaxxWiQ8=
2772-
dependencies:
2773-
is-extendable "^0.1.0"
2774-
27752768
external-editor@^3.0.3:
27762769
version "3.1.0"
27772770
resolved "https://registry.yarnpkg.com/external-editor/-/external-editor-3.1.0.tgz#cb03f740befae03ea4d283caed2741a83f335495"
@@ -3073,16 +3066,6 @@ graphql@^15.5.0:
30733066
resolved "https://registry.yarnpkg.com/graphql/-/graphql-15.5.0.tgz#39d19494dbe69d1ea719915b578bf920344a69d5"
30743067
integrity sha512-OmaM7y0kaK31NKG31q4YbD2beNYa6jBBKtMFT6gLYJljHLJr42IqJ8KX08u3Li/0ifzTU5HjmoOOrwa5BRLeDA==
30753068

3076-
gray-matter@^4.0.2:
3077-
version "4.0.2"
3078-
resolved "https://registry.yarnpkg.com/gray-matter/-/gray-matter-4.0.2.tgz#9aa379e3acaf421193fce7d2a28cebd4518ac454"
3079-
integrity sha512-7hB/+LxrOjq/dd8APlK0r24uL/67w7SkYnfwhNFwg/VDIGWGmduTDYf3WNstLW2fbbmRwrDGCVSJ2isuf2+4Hw==
3080-
dependencies:
3081-
js-yaml "^3.11.0"
3082-
kind-of "^6.0.2"
3083-
section-matter "^1.0.0"
3084-
strip-bom-string "^1.0.0"
3085-
30863069
handlebars@^4.7.7:
30873070
version "4.7.7"
30883071
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.7.7.tgz#9ce33416aad02dbd6c8fafa8240d5d98004945a1"
@@ -3277,11 +3260,6 @@ is-core-module@^2.2.0:
32773260
dependencies:
32783261
has "^1.0.3"
32793262

3280-
is-extendable@^0.1.0:
3281-
version "0.1.1"
3282-
resolved "https://registry.yarnpkg.com/is-extendable/-/is-extendable-0.1.1.tgz#62b110e289a471418e3ec36a617d472e301dfc89"
3283-
integrity sha1-YrEQ4omkcUGOPsNqYX1HLjAd/Ik=
3284-
32853263
is-extglob@^2.1.1:
32863264
version "2.1.1"
32873265
resolved "https://registry.yarnpkg.com/is-extglob/-/is-extglob-2.1.1.tgz#a88c02535791f02ed37c76a1b9ea9773c833f8c2"
@@ -3807,7 +3785,7 @@ jose@^4.11.4:
38073785
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
38083786
integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==
38093787

3810-
js-yaml@^3.11.0, js-yaml@^3.13.1:
3788+
js-yaml@^3.13.1:
38113789
version "3.14.1"
38123790
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.14.1.tgz#dae812fdb3825fa306609a8717383c50c36a0537"
38133791
integrity sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==
@@ -3884,11 +3862,6 @@ jsonify@~0.0.0:
38843862
resolved "https://registry.yarnpkg.com/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73"
38853863
integrity sha1-LHS27kHZPKUbe1qu6PUDYx0lKnM=
38863864

3887-
kind-of@^6.0.0, kind-of@^6.0.2:
3888-
version "6.0.3"
3889-
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-6.0.3.tgz#07c05034a6c349fa06e24fa35aa76db4580ce4dd"
3890-
integrity sha512-dcS1ul+9tmeD95T+x28/ehLgd9mENa3LsvDTtzm3vyBEO7RPptvAD+t44WVXaUjTBRcrpFeFlC8WCruUR456hw==
3891-
38923865
kleur@^3.0.3:
38933866
version "3.0.3"
38943867
resolved "https://registry.yarnpkg.com/kleur/-/kleur-3.0.3.tgz#a79c9ecc86ee1ce3fa6206d1216c501f147fc07e"
@@ -4649,14 +4622,6 @@ scuid@^1.1.0:
46494622
resolved "https://registry.yarnpkg.com/scuid/-/scuid-1.1.0.tgz#d3f9f920956e737a60f72d0e4ad280bf324d5dab"
46504623
integrity sha512-MuCAyrGZcTLfQoH2XoBlQ8C6bzwN88XT/0slOGz0pn8+gIP85BOAfYa44ZXQUTOwRwPU0QvgU+V+OSajl/59Xg==
46514624

4652-
section-matter@^1.0.0:
4653-
version "1.0.0"
4654-
resolved "https://registry.yarnpkg.com/section-matter/-/section-matter-1.0.0.tgz#e9041953506780ec01d59f292a19c7b850b84167"
4655-
integrity sha512-vfD3pmTzGpufjScBh50YHKzEu2lxBWhVEHsNGoEXmCmn2hKGfeNLYMzCJpe8cD7gqX7TJluOVpBkAequ6dgMmA==
4656-
dependencies:
4657-
extend-shallow "^2.0.1"
4658-
kind-of "^6.0.0"
4659-
46604625
semver@7.x, semver@^7.2.1:
46614626
version "7.3.5"
46624627
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.5.tgz#0b621c879348d8998e4b0e4be94b3f12e6018ef7"
@@ -4871,11 +4836,6 @@ strip-ansi@^6.0.1:
48714836
dependencies:
48724837
ansi-regex "^5.0.1"
48734838

4874-
strip-bom-string@^1.0.0:
4875-
version "1.0.0"
4876-
resolved "https://registry.yarnpkg.com/strip-bom-string/-/strip-bom-string-1.0.0.tgz#e5211e9224369fbb81d633a2f00044dc8cedad92"
4877-
integrity sha1-5SEekiQ2n7uB1jOi8ABE3IztrZI=
4878-
48794839
strip-bom@^4.0.0:
48804840
version "4.0.0"
48814841
resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-4.0.0.tgz#9c3505c1db45bcedca3d9cf7a16f5c5aa3901878"
@@ -5301,6 +5261,11 @@ yaml@^1.10.0:
53015261
resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.2.tgz#2301c5ffbf12b467de8da2333a459e29e7920e4b"
53025262
integrity sha512-r3vXyErRCYJ7wg28yvBY5VSoAF8ZvlcW9/BwUzEtUsjvX/DKs24dIkuwjtuprwJJHsbyUbLApepYTR1BN4uHrg==
53035263

5264+
yaml@^2.3.1:
5265+
version "2.3.1"
5266+
resolved "https://registry.yarnpkg.com/yaml/-/yaml-2.3.1.tgz#02fe0975d23cd441242aa7204e09fc28ac2ac33b"
5267+
integrity sha512-2eHWfjaoXgTBC2jNM1LRef62VQa0umtvRiDSk6HSzW7RvS5YtkabJrwYLLEKWBc8a5U2PTSCs+dJjUTJdlHsWQ==
5268+
53045269
yargs-parser@^18.1.2:
53055270
version "18.1.3"
53065271
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-18.1.3.tgz#be68c4975c6b2abf469236b0c870362fab09a7b0"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp