@inglorious/ssx 2.0.0 → 2.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.
- package/package.json +4 -4
- package/src/utils/markdown.js +16 -2
- package/src/utils/markdown.test.js +55 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/ssx",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"description": "Server-Side-X. Xecution? Xperience? Who knows.",
|
|
5
5
|
"author": "IceOnFire <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
|
|
6
6
|
"license": "MIT",
|
|
@@ -65,13 +65,13 @@
|
|
|
65
65
|
"vite": "^7.1.3",
|
|
66
66
|
"vite-plugin-image-optimizer": "^2.0.3",
|
|
67
67
|
"@inglorious/utils": "3.8.0",
|
|
68
|
-
"@inglorious/web": "5.0.
|
|
68
|
+
"@inglorious/web": "5.0.2"
|
|
69
69
|
},
|
|
70
70
|
"devDependencies": {
|
|
71
71
|
"prettier": "^3.6.2",
|
|
72
72
|
"vitest": "^1.6.1",
|
|
73
|
-
"@inglorious/
|
|
74
|
-
"@inglorious/
|
|
73
|
+
"@inglorious/logo": "3.0.2",
|
|
74
|
+
"@inglorious/eslint-config": "1.1.2"
|
|
75
75
|
},
|
|
76
76
|
"engines": {
|
|
77
77
|
"node": ">= 22"
|
package/src/utils/markdown.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import path from "node:path"
|
|
2
|
+
|
|
3
|
+
import { toCamelCase } from "@inglorious/utils/data-structures/string.js"
|
|
1
4
|
import hljs from "highlight.js"
|
|
2
5
|
import katex from "katex"
|
|
3
6
|
import MarkdownIt from "markdown-it"
|
|
@@ -24,6 +27,7 @@ export function markdownPlugin(options = {}) {
|
|
|
24
27
|
const matter = (await import("gray-matter")).default
|
|
25
28
|
const { content, data } = matter(code)
|
|
26
29
|
const htmlContent = md.render(content)
|
|
30
|
+
const moduleName = getMarkdownModuleName(id)
|
|
27
31
|
const hasMermaid =
|
|
28
32
|
content.includes('class="mermaid"') || content.includes("```mermaid")
|
|
29
33
|
|
|
@@ -40,8 +44,7 @@ export function markdownPlugin(options = {}) {
|
|
|
40
44
|
${mermaidCode}
|
|
41
45
|
|
|
42
46
|
export const metadata = ${JSON.stringify(data)}
|
|
43
|
-
|
|
44
|
-
export default {
|
|
47
|
+
export const ${moduleName} = {
|
|
45
48
|
render() {
|
|
46
49
|
if (typeof window !== "undefined" && ${hasMermaid}) {
|
|
47
50
|
setTimeout(() => {
|
|
@@ -100,3 +103,14 @@ function createMarkdownRenderer() {
|
|
|
100
103
|
|
|
101
104
|
return md
|
|
102
105
|
}
|
|
106
|
+
|
|
107
|
+
function getMarkdownModuleName(id) {
|
|
108
|
+
const baseName = path.basename(id, path.extname(id))
|
|
109
|
+
const pascalCase = toPascalCase(baseName)
|
|
110
|
+
return /^[A-Za-z_$]/.test(pascalCase) ? pascalCase : `_${pascalCase}`
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function toPascalCase(input) {
|
|
114
|
+
const camelCase = toCamelCase(input.replace(/[^a-zA-Z0-9_$-]/g, "_"))
|
|
115
|
+
return camelCase.replace(/^[a-z]/, (char) => char.toUpperCase())
|
|
116
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest"
|
|
2
|
+
|
|
3
|
+
import { markdownPlugin, renderMarkdown } from "./markdown.js"
|
|
4
|
+
|
|
5
|
+
describe("renderMarkdown", () => {
|
|
6
|
+
it("strips frontmatter and renders markdown content", () => {
|
|
7
|
+
const html = renderMarkdown(`---
|
|
8
|
+
title: Talk 2
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
# Hello World
|
|
12
|
+
|
|
13
|
+
This is a markdown page.
|
|
14
|
+
`)
|
|
15
|
+
|
|
16
|
+
expect(html).toContain("<h1>Hello World</h1>")
|
|
17
|
+
expect(html).toContain("<p>This is a markdown page.</p>")
|
|
18
|
+
expect(html).not.toContain("title: Talk 2")
|
|
19
|
+
})
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
describe("markdownPlugin", () => {
|
|
23
|
+
it("exports a unique named page module for each markdown file", async () => {
|
|
24
|
+
const plugin = markdownPlugin()
|
|
25
|
+
const code = `---
|
|
26
|
+
title: Talk 2
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
# Hello World
|
|
30
|
+
`
|
|
31
|
+
|
|
32
|
+
const result = await plugin.transform(
|
|
33
|
+
code,
|
|
34
|
+
"/Users/iceonfire/Projects/ic/my-app/src/pages/talk2.md",
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
expect(result).toContain(`export const metadata = {"title":"Talk 2"}`)
|
|
38
|
+
expect(result).toContain("export const Talk2 = {")
|
|
39
|
+
expect(result).not.toContain("export default")
|
|
40
|
+
expect(result).not.toContain("export const hydrate = false")
|
|
41
|
+
expect(result).toContain("unsafeHTML")
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it("converts kebab-case file names to PascalCase exports", async () => {
|
|
45
|
+
const plugin = markdownPlugin()
|
|
46
|
+
const code = `# Hello`
|
|
47
|
+
|
|
48
|
+
const result = await plugin.transform(
|
|
49
|
+
code,
|
|
50
|
+
"/Users/iceonfire/Projects/ic/my-app/src/pages/hello-world.md",
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
expect(result).toContain("export const HelloWorld = {")
|
|
54
|
+
})
|
|
55
|
+
})
|