@xyd-js/cli 0.1.0-xyd.34 → 0.1.0-xyd.37

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.
@@ -1,194 +0,0 @@
1
- import path from "node:path";
2
-
3
- import React from "react";
4
- import {redirect} from "react-router";
5
-
6
- import {PageFrontMatter} from "@xyd-js/core"
7
- import {compileBySlug} from "@xyd-js/content"
8
- import {mapSettingsToProps} from "@xyd-js/framework/hydration";
9
- import {Framework, FwNav, type FwSidebarGroupProps} from "@xyd-js/framework/react";
10
- import getContentComponents from "@xyd-js/components/content";
11
- import {HomePage} from "@xyd-js/components/pages";
12
- import type {IBreadcrumb, INavLinks} from "@xyd-js/ui";
13
-
14
- import settings from 'virtual:xyd-settings';
15
- import Theme from "virtual:xyd-theme";
16
-
17
- import "virtual:xyd-theme/index.css"
18
- import "virtual:xyd-theme-override/index.css"
19
-
20
- interface loaderData {
21
- sidebarGroups: FwSidebarGroupProps[]
22
- breadcrumbs: IBreadcrumb[],
23
- navlinks?: INavLinks,
24
- toc: PageFrontMatter
25
- slug: string
26
- code: string
27
- }
28
-
29
- const contentComponents = {
30
- ...getContentComponents(),
31
-
32
- HomePage: (props) => <HomePage
33
- {...props}
34
- // TODO: get props from theme about nav (middle etc)
35
- // TODO: footer
36
- // TODO: style
37
- header={<div style={{marginLeft: "var(--xyd-global-page-gutter)"}}>
38
- <FwNav kind="middle"/>
39
- </div>}
40
-
41
- >
42
- {props.children}
43
- </HomePage>
44
- }
45
-
46
- const supportedExtensions = {
47
- ".mdx": true,
48
- ".md": true,
49
- "": true,
50
- }
51
-
52
- function getPathname(url: string) {
53
- const parsedUrl = new URL(url);
54
- return parsedUrl.pathname.replace(/^\//, '');
55
- }
56
-
57
- export async function loader({request}: { request: any }) {
58
- const slug = getPathname(request.url || "index") || "index"
59
- const ext = path.extname(slug)
60
-
61
- if (!supportedExtensions[ext]) {
62
- return {}
63
- }
64
-
65
- // TODO: in the future map instead of arr
66
- if (settings.redirects && settings.redirects.length) {
67
- for (const item of settings.redirects) {
68
- if (item.source === getPathname(request.url)) {
69
- return redirect(item.destination)
70
- }
71
- }
72
- }
73
-
74
- let code = ""
75
- let error: any
76
-
77
- try {
78
- code = await compileBySlug(slug, true)
79
- } catch (e) {
80
- error = e
81
- }
82
-
83
- if (error?.code === "ENOENT") {
84
- try {
85
- // TODO: better index algorithm
86
- code = await compileBySlug(slug + "/index", true)
87
- } catch (e) {
88
- error = e
89
- }
90
- }
91
-
92
- const {
93
- groups: sidebarGroups,
94
- breadcrumbs,
95
- navlinks,
96
- } = await mapSettingsToProps(
97
- settings,
98
- slug
99
- )
100
-
101
- if (error) {
102
- if (sidebarGroups && error.code === "ENOENT") {
103
- const firstItem = sidebarGroups?.[0]?.items?.[0]
104
-
105
- if (firstItem) {
106
- return redirect(firstItem.href)
107
- }
108
- }
109
-
110
- console.error(error)
111
- }
112
-
113
- console.timeEnd("docs loader")
114
- return {
115
- sidebarGroups,
116
- breadcrumbs,
117
- navlinks,
118
- slug,
119
- code,
120
- } as loaderData
121
- }
122
-
123
- // TODO: move to content?
124
- function mdxExport(code: string) {
125
- const scope = {
126
- Fragment: React.Fragment,
127
- jsxs: React.createElement,
128
- jsx: React.createElement,
129
- jsxDEV: React.createElement,
130
- }
131
- const fn = new Function(...Object.keys(scope), code)
132
- return fn(scope)
133
- }
134
-
135
- // // TODO: move to content?
136
- function mdxContent(code: string) {
137
- const content = mdxExport(code) // TODO: fix any
138
- if (!mdxExport) {
139
- return {}
140
- }
141
-
142
- return {
143
- component: content?.default,
144
- toc: content?.toc,
145
- frontmatter: content?.frontmatter,
146
- themeSettings: content?.themeSettings || {},
147
- page: content?.page || false,
148
- }
149
- }
150
-
151
- export function MemoMDXComponent(codeComponent: any) {
152
- return React.useMemo(
153
- () => codeComponent ? codeComponent : null,
154
- [codeComponent]
155
- )
156
- }
157
-
158
- export default function CustomPage({loaderData, ...rest}: { loaderData: loaderData }) {
159
- const content = mdxContent(loaderData.code)
160
- const Content = MemoMDXComponent(content.component)
161
-
162
- if (!Content) {
163
- console.error("Content not found")
164
- return null
165
- }
166
-
167
- let component: React.JSX.Element
168
-
169
- if (content.page) {
170
- component = <Content
171
- components={{
172
- ...contentComponents,
173
- }}
174
- />
175
- } else {
176
- component = <Theme>
177
- <Content
178
- components={{
179
- ...contentComponents,
180
- }}
181
- />
182
- </Theme>
183
- }
184
-
185
- return <Framework
186
- settings={settings}
187
- sidebarGroups={loaderData.sidebarGroups || []}
188
- toc={content.toc || []}
189
- breadcrumbs={loaderData.breadcrumbs || []}
190
- navlinks={loaderData.navlinks}
191
- >
192
- {component}
193
- </Framework>
194
- }
@@ -1,37 +0,0 @@
1
- import {
2
- Links,
3
- Meta,
4
- Outlet,
5
- Scripts,
6
- } from "react-router";
7
-
8
- // TODO: config from core settings
9
- export const meta = () => {
10
- return [
11
- {
12
- title: "xyd"
13
- }
14
- ]
15
- }
16
-
17
- export const Layout = ({children}: { children: React.ReactNode }) => {
18
- return (
19
- <html>
20
- <head>
21
- <meta charSet="utf-8"/>
22
- <meta name="viewport" content="width=device-width, initial-scale=1"/>
23
- <Meta/>
24
- <Links/>
25
- </head>
26
- <body>
27
- {children}
28
- <Scripts/>
29
- </body>
30
- </html>
31
- )
32
- }
33
-
34
- export default function App() {
35
- return <Outlet/>
36
- }
37
-