@dropsh/plugin-markdown 0.5.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.
- package/LICENSE +51 -0
- package/package.json +26 -0
- package/src/index.ts +15 -0
- package/src/render-md.ts +80 -0
- package/tests/unit/render-md.test.ts +108 -0
- package/tsconfig.json +8 -0
- package/vitest.config.ts +14 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
drupal-cli is dual-licensed. You may use this software under the terms of
|
|
2
|
+
either the MIT License or the GNU General Public License version 2 or any
|
|
3
|
+
later version, at your option.
|
|
4
|
+
|
|
5
|
+
SPDX-License-Identifier: (MIT OR GPL-2.0-or-later)
|
|
6
|
+
|
|
7
|
+
================================================================================
|
|
8
|
+
MIT License
|
|
9
|
+
================================================================================
|
|
10
|
+
|
|
11
|
+
Copyright (c) 2026 Christian Wiedemann
|
|
12
|
+
|
|
13
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
14
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
15
|
+
in the Software without restriction, including without limitation the rights
|
|
16
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
17
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
18
|
+
furnished to do so, subject to the following conditions:
|
|
19
|
+
|
|
20
|
+
The above copyright notice and this permission notice shall be included in all
|
|
21
|
+
copies or substantial portions of the Software.
|
|
22
|
+
|
|
23
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
24
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
25
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
26
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
27
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
28
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
29
|
+
SOFTWARE.
|
|
30
|
+
|
|
31
|
+
================================================================================
|
|
32
|
+
GNU General Public License, version 2 or later
|
|
33
|
+
================================================================================
|
|
34
|
+
|
|
35
|
+
The full text of the GNU GPL v2 is available at:
|
|
36
|
+
https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
|
|
37
|
+
|
|
38
|
+
The full text of any later GPL version is available at:
|
|
39
|
+
https://www.gnu.org/licenses/
|
|
40
|
+
|
|
41
|
+
Copyright (C) 2026 Christian Wiedemann
|
|
42
|
+
|
|
43
|
+
This program is free software; you can redistribute it and/or modify it under
|
|
44
|
+
the terms of the GNU General Public License as published by the Free Software
|
|
45
|
+
Foundation; either version 2 of the License, or (at your option) any later
|
|
46
|
+
version.
|
|
47
|
+
|
|
48
|
+
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
49
|
+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
50
|
+
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
51
|
+
details.
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dropsh/plugin-markdown",
|
|
3
|
+
"version": "0.5.2",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/plugins/markdown/src/index.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./dist/plugins/markdown/src/index.js",
|
|
8
|
+
"./render": "./dist/plugins/markdown/src/render-md.js"
|
|
9
|
+
},
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"peerDependencies": {
|
|
14
|
+
"dropsh": "*"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"typescript": "^5.6.0",
|
|
18
|
+
"vitest": "^2.1.0",
|
|
19
|
+
"dropsh": "0.5.2"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsc",
|
|
23
|
+
"test": "vitest run --passWithNoTests",
|
|
24
|
+
"typecheck": "tsc --noEmit"
|
|
25
|
+
}
|
|
26
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { DropSHPlugin } from "dropsh/plugin";
|
|
2
|
+
import { renderMarkdown } from "./render-md.js";
|
|
3
|
+
|
|
4
|
+
export { renderMarkdown };
|
|
5
|
+
|
|
6
|
+
export function markdownPlugin(): DropSHPlugin {
|
|
7
|
+
return {
|
|
8
|
+
id: "markdown",
|
|
9
|
+
requiredModules: [],
|
|
10
|
+
async extendSchema(_entityType, _bundle, schema) {
|
|
11
|
+
return schema;
|
|
12
|
+
},
|
|
13
|
+
renderers: [{ id: "md", render: renderMarkdown }],
|
|
14
|
+
};
|
|
15
|
+
}
|
package/src/render-md.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import {
|
|
2
|
+
indexIncluded,
|
|
3
|
+
type JsonApiDocument,
|
|
4
|
+
type JsonApiResource,
|
|
5
|
+
type RenderContext,
|
|
6
|
+
} from "dropsh/plugin";
|
|
7
|
+
|
|
8
|
+
type Scalar = string | number | boolean;
|
|
9
|
+
|
|
10
|
+
function isScalar(v: unknown): v is Scalar {
|
|
11
|
+
return typeof v === "string" || typeof v === "number" || typeof v === "boolean";
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function yamlScalar(v: Scalar): string {
|
|
15
|
+
if (typeof v === "string") {
|
|
16
|
+
return v === "" || v.trim() !== v || /[:#\n"'[\]{}]/.test(v) ? JSON.stringify(v) : v;
|
|
17
|
+
}
|
|
18
|
+
return String(v);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function isRef(x: unknown): x is { type: string; id: string } {
|
|
22
|
+
return (
|
|
23
|
+
!!x &&
|
|
24
|
+
typeof x === "object" &&
|
|
25
|
+
typeof (x as { type?: unknown }).type === "string" &&
|
|
26
|
+
typeof (x as { id?: unknown }).id === "string"
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function relIds(rel: unknown): string[] {
|
|
31
|
+
if (!rel || typeof rel !== "object") return [];
|
|
32
|
+
const data = (rel as { data?: unknown }).data;
|
|
33
|
+
if (Array.isArray(data)) return data.filter(isRef).map((r) => `${r.type}/${r.id}`);
|
|
34
|
+
if (isRef(data)) return [`${data.type}/${data.id}`];
|
|
35
|
+
return [];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Flattens any attribute value to a single-line string for `label: value`
|
|
39
|
+
// output. Text-field objects (`{ processed | value }`) collapse to their text;
|
|
40
|
+
// other objects/arrays fall back to compact JSON.
|
|
41
|
+
function fieldValue(v: unknown): string {
|
|
42
|
+
if (isScalar(v)) return yamlScalar(v);
|
|
43
|
+
if (v && typeof v === "object") {
|
|
44
|
+
const o = v as Record<string, unknown>;
|
|
45
|
+
if (typeof o.processed === "string") return yamlScalar(o.processed);
|
|
46
|
+
if (typeof o.value === "string") return yamlScalar(o.value);
|
|
47
|
+
return JSON.stringify(v);
|
|
48
|
+
}
|
|
49
|
+
return "";
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Renders a resource as a flat `label: value` list: type/id first, then every
|
|
53
|
+
// attribute, then relationships as `[type/id]` reference lists.
|
|
54
|
+
function renderResource(res: JsonApiResource): string {
|
|
55
|
+
const lines: string[] = [`type: ${res.type}`, `id: ${res.id}`];
|
|
56
|
+
for (const [k, v] of Object.entries(res.attributes ?? {})) {
|
|
57
|
+
lines.push(`${k}: ${fieldValue(v)}`);
|
|
58
|
+
}
|
|
59
|
+
for (const [k, v] of Object.entries(res.relationships ?? {})) {
|
|
60
|
+
const ids = relIds(v);
|
|
61
|
+
if (ids.length) lines.push(`${k}: [${ids.join(", ")}]`);
|
|
62
|
+
}
|
|
63
|
+
return lines.join("\n");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function renderMarkdown(doc: JsonApiDocument, _ctx: RenderContext): string {
|
|
67
|
+
const data = doc.data;
|
|
68
|
+
const primary = Array.isArray(data)
|
|
69
|
+
? data.length === 0
|
|
70
|
+
? "_(no results)_"
|
|
71
|
+
: data.map(renderResource).join("\n\n---\n\n")
|
|
72
|
+
: renderResource(data);
|
|
73
|
+
|
|
74
|
+
// Resources pulled in via `--include` are appended under an Included section
|
|
75
|
+
// so the related entities are visible, not just their reference ids.
|
|
76
|
+
const included = [...indexIncluded(doc).values()];
|
|
77
|
+
if (included.length === 0) return primary;
|
|
78
|
+
const inc = included.map(renderResource).join("\n\n---\n\n");
|
|
79
|
+
return `${primary}\n\n## Included\n\n${inc}`;
|
|
80
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { renderMarkdown } from "../../src/render-md.js";
|
|
3
|
+
|
|
4
|
+
const ctx = { command: "read" as const };
|
|
5
|
+
|
|
6
|
+
describe("renderMarkdown", () => {
|
|
7
|
+
it("appends an Included section for --include'd resources", () => {
|
|
8
|
+
const out = renderMarkdown(
|
|
9
|
+
{
|
|
10
|
+
data: {
|
|
11
|
+
type: "node--article",
|
|
12
|
+
id: "u1",
|
|
13
|
+
attributes: { title: "Primary" },
|
|
14
|
+
relationships: { field_related: { data: { type: "node--article", id: "r1" } } },
|
|
15
|
+
},
|
|
16
|
+
included: [{ type: "node--article", id: "r1", attributes: { title: "Related One" } }],
|
|
17
|
+
},
|
|
18
|
+
ctx,
|
|
19
|
+
);
|
|
20
|
+
expect(out).toContain("title: Primary");
|
|
21
|
+
expect(out).toContain("## Included");
|
|
22
|
+
expect(out).toContain("id: r1");
|
|
23
|
+
expect(out).toContain("title: Related One");
|
|
24
|
+
// primary comes before the included section
|
|
25
|
+
expect(out.indexOf("title: Primary")).toBeLessThan(out.indexOf("## Included"));
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("omits the Included section when there is no included", () => {
|
|
29
|
+
const out = renderMarkdown(
|
|
30
|
+
{ data: { type: "node--article", id: "u1", attributes: { title: "Solo" } } },
|
|
31
|
+
ctx,
|
|
32
|
+
);
|
|
33
|
+
expect(out).not.toContain("## Included");
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("renders every field as label: value", () => {
|
|
37
|
+
const out = renderMarkdown(
|
|
38
|
+
{
|
|
39
|
+
data: {
|
|
40
|
+
type: "node--article",
|
|
41
|
+
id: "u1",
|
|
42
|
+
attributes: { title: "Hello", status: true, body: { value: "The text." } },
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
ctx,
|
|
46
|
+
);
|
|
47
|
+
expect(out).toContain("type: node--article");
|
|
48
|
+
expect(out).toContain("id: u1");
|
|
49
|
+
expect(out).toContain("title: Hello");
|
|
50
|
+
expect(out).toContain("status: true");
|
|
51
|
+
// text-field objects collapse to their text, still as label: value
|
|
52
|
+
expect(out).toContain("body: The text.");
|
|
53
|
+
// no frontmatter fence
|
|
54
|
+
expect(out.startsWith("---")).toBe(false);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("does not duplicate any field when there is no body field", () => {
|
|
58
|
+
const out = renderMarkdown(
|
|
59
|
+
{
|
|
60
|
+
data: {
|
|
61
|
+
type: "node--page",
|
|
62
|
+
id: "p1",
|
|
63
|
+
attributes: { title: "T", summary: "short", teaser: "a much longer piece of text here" },
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
ctx,
|
|
67
|
+
);
|
|
68
|
+
expect(out).toContain("title: T");
|
|
69
|
+
expect(out).toContain("summary: short");
|
|
70
|
+
expect(out).toContain("teaser: a much longer piece of text here");
|
|
71
|
+
// each field appears exactly once (no body-fallback duplication)
|
|
72
|
+
expect(out.match(/teaser: /g)).toHaveLength(1);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("lists relationships as type/id arrays", () => {
|
|
76
|
+
const out = renderMarkdown(
|
|
77
|
+
{
|
|
78
|
+
data: {
|
|
79
|
+
type: "node--article",
|
|
80
|
+
id: "u1",
|
|
81
|
+
attributes: {},
|
|
82
|
+
relationships: { uid: { data: { type: "user--user", id: "a1" } } },
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
ctx,
|
|
86
|
+
);
|
|
87
|
+
expect(out).toContain("uid: [user--user/a1]");
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("joins a collection with a separator", () => {
|
|
91
|
+
const out = renderMarkdown(
|
|
92
|
+
{
|
|
93
|
+
data: [
|
|
94
|
+
{ type: "node--article", id: "u1", attributes: { title: "A" } },
|
|
95
|
+
{ type: "node--article", id: "u2", attributes: { title: "B" } },
|
|
96
|
+
],
|
|
97
|
+
},
|
|
98
|
+
{ command: "search" },
|
|
99
|
+
);
|
|
100
|
+
expect(out).toContain("id: u1");
|
|
101
|
+
expect(out).toContain("id: u2");
|
|
102
|
+
expect(out).toContain("\n\n---\n\n");
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("renders an empty collection as a placeholder", () => {
|
|
106
|
+
expect(renderMarkdown({ data: [] }, { command: "search" })).toBe("_(no results)_");
|
|
107
|
+
});
|
|
108
|
+
});
|
package/tsconfig.json
ADDED
package/vitest.config.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
import { defineConfig } from "vitest/config";
|
|
3
|
+
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
resolve: {
|
|
6
|
+
alias: {
|
|
7
|
+
"dropsh/plugin": resolve(__dirname, "../../src/plugin-api.ts"),
|
|
8
|
+
},
|
|
9
|
+
},
|
|
10
|
+
test: {
|
|
11
|
+
include: ["tests/unit/**/*.test.ts"],
|
|
12
|
+
environment: "node",
|
|
13
|
+
},
|
|
14
|
+
});
|