-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.tsx
More file actions
279 lines (272 loc) · 8.75 KB
/
utils.tsx
File metadata and controls
279 lines (272 loc) · 8.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* eslint-disable react/display-name */
import React, { useEffect, useState } from 'react'
import { PodNode, Rules, getTextContentFromNode, makeAttrs, setFn, PodliteDocument } from '@podlite/schema'
import { Editor2 as Editor, HighlightedCode, WindowWrapper } from '@podlite/editor-react'
import ReactDOMServer from 'react-dom/server'
import Podlite from '@podlite/to-jsx'
import * as img from '../built/images'
//@ts-ignore
import * as components from '../built/components'
import Link from 'next/link'
import { SiteInfo } from '@podlite/publisher/lib/site-data-plugin'
import { pubRecord } from '@podlite/publisher'
// POSTS_PATH is useful when you want to get the path to a specific file
// type pubRecord = {
// type: string
// pubdate: string | number
// node: PodNode
// description: PodNode
// file: string
// }
type publishRecord = Omit<pubRecord, 'pubdate'> & {
title: string | null
publishUrl: string
sources: string[]
node: PodliteDocument
pubdate?: string | null
}
export type DataFeed = {
all: FeedContent[]
control: {
[propName: string]: string
}
imagesMap: {
[path: string]: string
}
}
export type FeedContent = publishRecord & {
title: string | null
number?: number
sxd: string
sequence?: number
slug?: string
sources?: string[]
shortUrl?: string
}
export type ContentRecord = Pick<FeedContent, 'publishUrl' | 'title' | 'node' | 'sources' | 'shortUrl'>
export type DataFeedContent = {
all: publishRecord[]
siteInfo: SiteInfo
control: {
stateVersion: any
nextPublishTime: any
}
imagesMap: {
[k: string]: any
}
}
export function getSiteInfo(): DataFeedContent['siteInfo'] {
const d = require('../built/siteInfo.json')
return d as DataFeedContent['siteInfo']
}
export function mapPathToImage(path: string): string | undefined {
const data = require('../built/imagesMap.json') as DataFeedContent['imagesMap']
return data[path]
}
export function getArticlesGroupedByYearMonth(pages: publishRecord[]) {
//filter out pages
const source = pages.filter(({ type = '' }) => type !== 'page').reverse()
const groupedByYearMonth = source.reduce(
(
acc: {
[year: number]: {
[month: number]: publishRecord[]
}
},
rest,
) => {
const year = new Date(rest.pubdate ? rest.pubdate : new Date()).getFullYear()
const month = new Date(rest.pubdate ? rest.pubdate : new Date()).getMonth()
const { [year]: months = {}, ...other } = acc
const { [month]: monthRecords = [], ...otherMonth } = months
return {
...other,
[year]: {
...otherMonth,
[month]: [...monthRecords, rest],
},
}
},
{},
)
return groupedByYearMonth
}
export function getPostComponent(podNode: PodNode, template?: publishRecord, opt?: any) {
const plugins = (makeComponent): Partial<Rules> => {
const mkComponent = src => (writer, processor) => (node, ctx, interator) => {
// check if node.content defined
return makeComponent(src, node, 'content' in node ? interator(node.content, { ...ctx }) : [])
}
const hcode = mkComponent(({ children, key, ...node }, ctx) => (
<HighlightedCode node={node} keyProp={key} ctx={ctx}>
{children}
</HighlightedCode>
))
return {
//process only content nodes
root: () => (node, ctx, interator) => {
return interator(node.content, { ...ctx })
},
//process only content nodes
pod: () => (node, ctx, interator) => {
return interator(node.content, { ...ctx })
},
//@ts-ignore
TITLE: () => () => null,
//@ts-ignore
SUBTITLE: () => () => null,
//@ts-ignore
FOOTER: () => () => null,
//@ts-ignore
HEADER: () => () => null,
//@ts-ignore
DESCRIPTION: () => () => null,
'L<>': setFn((node, ctx) => {
let { meta } = node
if (meta === null) {
meta = getTextContentFromNode(node)
}
const text_content = getTextContentFromNode(node)
//TODO: fix links to anchors
return mkComponent(({ children, key }) => (
<Link href={meta?.trim().replace(/\s/g, '-') || '#'} key={key}>
{text_content}
{/* {Array.isArray(children) ? children[0] : children} */}
</Link>
))
}),
React: () => (node, ctx, interator) => {
const conf = makeAttrs(node, ctx)
const componentName = conf.getFirstValue('component')
const variables = Object.keys(conf.asHash()).filter(name => name !== 'component')
// prepare simple key value pairs
const props = variables.reduce((acc, name) => {
acc[name] = conf.getFirstValue(name)
return acc
}, {})
if (components[componentName]) {
return makeComponent(
components[componentName],
{
...props,
...{ item: template },
...{ renderNode: node => getPostComponent(node, template), getThisNode: () => node, getOpt: () => opt },
},
interator(node.content, ctx),
)
} else {
return (
<div style={{ color: 'red' }}>
{' '}
Not found:{' '}
<code>
<pre>{componentName}</pre>
</code>
</div>
)
}
},
PodLite: setFn((node, ctx) => {
const conf = makeAttrs(node, ctx)
const variables = Object.keys(conf.asHash()).filter(name => name !== 'component')
const props = variables.reduce((acc, name) => {
acc[name] = conf.getFirstValue(name)
return acc
}, {})
const caption = conf.exists('caption') ? conf.getFirstValue('caption') : null
const size = conf.exists('size') ? conf.getFirstValue('size') : null
// :!enablePreview or :enablePreview
const enablePreview = conf.exists('enablePreview') ? conf.getFirstValue('enablePreview') : true
const enableAutocompletion = conf.exists('enableAutocompletion')
? conf.getFirstValue('enableAutocompletion')
: false
const enableFolding = conf.exists('enableFolding') ? conf.getFirstValue('enableFolding') : false
const text = getTextContentFromNode(node)
return mkComponent(({ children, key }) => (
<div className={`editor-block ${size}`} key={`${key}-code-div`}>
<WindowWrapper title={caption}>
<Editor
height={'500px'}
value={text}
enablePreview={enablePreview}
previewWidth={'50%'}
basicSetup={{ defaultKeymap: false }}
readOnly={false}
isFullscreen={false}
enableAutocompletion={enableAutocompletion}
enableFolding={enableFolding}
{...props}
/>
</WindowWrapper>
</div>
))
}),
code1: () => (node, ctx, interator) => {
const conf = makeAttrs(node, ctx)
const code = getTextContentFromNode(node)
const lang = conf.getFirstValue('lang')
return (
<pre>
<code className={lang}>{code}</code>
</pre>
)
},
':code': hcode,
code: hcode,
}
}
const node = {
interator: podNode,
errors: {},
toString: () => '',
valueOf: () => '',
indexingTerms: {},
annotations: {},
defenitions: {},
}
// wrap all elements and add line link info
const wrapFunction = (node: PodNode, children, ctx) => {
if (typeof node !== 'string' && 'type' in node && node.type == 'image') {
const imageName = node.src
const linkTo = ctx.link
if (imageName && img[imageName]) {
const placeholder = !node.src.endsWith('gif') ? 'blur' : 'empty'
const Comp = () => {
if (node.src.endsWith('mp4')) {
return (
<div className="video shadow">
{' '}
<video controls>
{' '}
<source src={img[imageName]} type="video/mp4" />{' '}
</video>
</div>
)
} else {
return <img className="shadow_DISABLED" src={img[imageName]} />
}
}
return linkTo ? (
<a target="_blank" rel="noreferrer" href={linkTo}>
<Comp />
</a>
) : (
<Comp />
)
} else {
return (
<>
<h1>image {imageName} not found</h1>
</>
)
}
return children
} else {
return children
}
}
return <Podlite plugins={plugins} tree={node} wrapElement={wrapFunction} />
}
export function convertPodNodeToHtml(node: PodNode): string {
return ReactDOMServer.renderToString(getPostComponent(node))
}