@kokiito0926/fmeditor 0.0.0 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +9 -4
  2. package/index.js +61 -34
  3. package/package.json +5 -4
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  ## fmeditor
2
2
 
3
3
  fmeditorは、フロントマターを編集することができるコマンドラインのツールです。
4
- 標準入力からマークダウンを受け取り、フロントマターのフィールドをコマンドライン引数から編集することができます。
4
+ マークダウンを標準入力から受け取り、コマンドライン引数に従って、フロントマターを編集します。
5
5
 
6
6
  ## インストール
7
7
 
@@ -11,14 +11,19 @@ $ npm install --global @kokiito0926/fmeditor
11
11
 
12
12
  ## 使用方法
13
13
 
14
- curlなどで取得したマークダウンをパイプでfmeditorに流し込みます。
14
+ curlなどで取得したマークダウンをパイプでfmeditorに流し込みます。
15
+ そうすると、コマンドライン引数の値がセットされたフロントマターとともにマークダウンが出力されます。
15
16
 
16
17
  ```bash
17
- $ curl -fsSL https://raw.githubusercontent.com/Kernix13/markdown-cheatsheet/refs/heads/master/frontmatter.md | fmeditor --draft true
18
+ $ curl -fsSL https://raw.githubusercontent.com/Kernix13/markdown-cheatsheet/refs/heads/master/frontmatter.md | fmeditor get --title --tags --draft
18
19
  ```
19
20
 
20
21
  ```bash
21
- $ curl -fsSL https://raw.githubusercontent.com/Kernix13/markdown-cheatsheet/refs/heads/master/frontmatter.md | fmeditor --tags '["markdown", "frontmatter", "yaml"]'
22
+ $ curl -fsSL https://raw.githubusercontent.com/Kernix13/markdown-cheatsheet/refs/heads/master/frontmatter.md | fmeditor set --tags '["markdown", "frontmatter", "yaml"]'
23
+ ```
24
+
25
+ ```bash
26
+ $ curl -fsSL https://raw.githubusercontent.com/Kernix13/markdown-cheatsheet/refs/heads/master/frontmatter.md | fmeditor remove --title --tags
22
27
  ```
23
28
 
24
29
  ## ライセンス
package/index.js CHANGED
@@ -1,34 +1,61 @@
1
- #!/usr/bin/env node
2
-
3
- // >> $ curl -fsSL https://raw.githubusercontent.com/Kernix13/markdown-cheatsheet/refs/heads/master/frontmatter.md | ./index.js --draft true
4
- // >> $ curl -fsSL https://raw.githubusercontent.com/Kernix13/markdown-cheatsheet/refs/heads/master/frontmatter.md | ./index.js --tags '["markdown", "frontmatter", "yaml"]'
5
-
6
- import { stdin, argv } from "zx";
7
- import matter from "gray-matter";
8
-
9
- if (process.stdin.isTTY) {
10
- process.exit(1);
11
- }
12
-
13
- const input = await stdin();
14
- if (!input) {
15
- process.exit(1);
16
- }
17
-
18
- const { _, ...flags } = argv;
19
-
20
- const processedData = {};
21
- for (const [key, value] of Object.entries(flags)) {
22
- try {
23
- processedData[key] = JSON.parse(value);
24
- } catch (err) {
25
- processedData[key] = value;
26
- }
27
- }
28
-
29
- const doc = matter(input || "");
30
-
31
- doc.data = { ...doc.data, ...processedData };
32
-
33
- const text = matter.stringify(doc.content, doc.data);
34
- console.log(text);
1
+ #!/usr/bin/env node
2
+
3
+ // >> $ curl -fsSL https://raw.githubusercontent.com/Kernix13/markdown-cheatsheet/refs/heads/master/frontmatter.md | ./index.js get --title --tags --draft
4
+ // >> $ curl -fsSL https://raw.githubusercontent.com/Kernix13/markdown-cheatsheet/refs/heads/master/frontmatter.md | ./index.js set --tags '["markdown", "frontmatter", "yaml"]'
5
+ // >> $ curl -fsSL https://raw.githubusercontent.com/Kernix13/markdown-cheatsheet/refs/heads/master/frontmatter.md | ./index.js remove --title --tags
6
+
7
+ import { stdin, argv } from "zx";
8
+ import matter from "gray-matter";
9
+ import yaml from "js-yaml";
10
+
11
+ if (process.stdin.isTTY) {
12
+ process.exit(1);
13
+ }
14
+
15
+ const input = await stdin();
16
+ if (!input) {
17
+ process.exit(1);
18
+ }
19
+
20
+ const { _, ...flags } = argv;
21
+ const command = _[0] || "set";
22
+
23
+ if (command === "set") {
24
+ const processedData = {};
25
+ for (const [key, value] of Object.entries(flags)) {
26
+ try {
27
+ processedData[key] = JSON.parse(value);
28
+ } catch (err) {
29
+ processedData[key] = value;
30
+ }
31
+ }
32
+
33
+ const doc = matter(input);
34
+
35
+ doc.data = { ...doc.data, ...processedData };
36
+
37
+ const text = matter.stringify(doc.content, doc.data);
38
+ console.log(text);
39
+ } else if (command === "get") {
40
+ const doc = matter(input);
41
+
42
+ const keys = Object.keys(flags);
43
+ if (keys.length === 0) {
44
+ console.log(yaml.dump(doc.data));
45
+ } else {
46
+ const result = {};
47
+ keys.forEach((k) => {
48
+ if (k in doc.data) result[k] = doc.data[k];
49
+ });
50
+ console.log(yaml.dump(result));
51
+ }
52
+ } else if (command === "remove") {
53
+ const doc = matter(input);
54
+
55
+ Object.keys(flags).forEach((k) => {
56
+ delete doc.data[k];
57
+ });
58
+
59
+ const text = matter.stringify(doc.content, doc.data);
60
+ console.log(text);
61
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kokiito0926/fmeditor",
3
- "version": "0.0.0",
3
+ "version": "0.0.2",
4
4
  "private": false,
5
5
  "description": "フロントマターを編集することができるコマンドラインのツールです。",
6
6
  "keywords": [
@@ -34,10 +34,11 @@
34
34
  "test": "node --test"
35
35
  },
36
36
  "dependencies": {
37
- "zx": "^8.8.4",
38
- "gray-matter": "^4.0.3"
37
+ "gray-matter": "^4.0.3",
38
+ "js-yaml": "^4.1.1",
39
+ "zx": "^8.8.4"
39
40
  },
40
41
  "engines": {
41
42
  "node": ">=24.0.0"
42
43
  }
43
- }
44
+ }