-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate.ts
149 lines (140 loc) · 3.85 KB
/
generate.ts
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
import { ContentConverter, Unified } from 'docube'
import { makeMarkdownConverter } from '@docube/markdown'
import {
makeTransformer,
makeAppConfig,
LoaderLive,
makeOutputMeta,
FileConverterLive,
ContentValidatorLive,
ModuleResolverLive,
} from '@docube/common'
import remarkGfm from 'remark-gfm'
import { Effect, Layer } from 'effect'
import rehypeShiftHeading from 'rehype-shift-heading'
import rehypePrettyCode from 'rehype-pretty-code'
import { transformerCopyButton } from '@rehype-pretty/transformers'
import slug from 'rehype-slug-custom-id'
import parse from 'uniorg-parse'
import uniorg2rehype from 'uniorg-rehype'
import stringify from 'rehype-stringify'
import extractKeywords from 'uniorg-extract-keywords'
import { unified } from 'unified'
import raw from 'rehype-raw'
import rehypeAutolinkHeadings from 'rehype-autolink-headings'
import rehypeKatex from 'rehype-katex'
import rehypeCallouts from 'rehype-callouts'
import rehypeProbeImageSize from './lib/rehypeImage'
const AppConfigLive = makeAppConfig({
name: 'Post',
directory: './posts',
include: '**/*.{md,org}',
fields: (s) => ({
title: s.String,
tags: s.Array(s.String),
series: s.String,
createdAt: s.String,
publishedAt: s.String,
updatedAt: s.optional(s.String),
summary: s.String,
}),
})
const rehypePrettyOptions = {
theme: {
light: 'material-theme-lighter',
dark: 'nord',
},
bypassInlineCode: true,
transformers: [transformerCopyButton()],
}
const orgProcessor = unified()
.use(parse)
.use(extractKeywords)
.use(uniorg2rehype, {
imageFilenameExtensions: [
'png',
'jpeg',
'jpg',
'gif',
'tiff',
'tif',
'xbm',
'xpm',
'pbm',
'pgm',
'ppm',
'pnm',
'svg',
'webp',
],
})
// @ts-ignore
.use(raw)
.use(rehypeProbeImageSize)
.use(rehypeShiftHeading, { shift: 1 })
.use(rehypePrettyCode, rehypePrettyOptions as any)
.use(slug)
.use(rehypeAutolinkHeadings, { behavior: 'wrap' })
.use(rehypeKatex)
.use(rehypeCallouts)
.use(stringify)
const UnifiedLive = Layer.succeed(
Unified,
Unified.of({
process(content) {
return Effect.promise(() => orgProcessor.process(content))
},
})
)
const MarkdownConverterLive = makeMarkdownConverter({
remarkPlugins: [remarkGfm],
rehypePlugins: [
rehypeProbeImageSize,
[rehypePrettyCode, rehypePrettyOptions],
slug,
[rehypeAutolinkHeadings, { behavior: 'wrap' }],
],
})
const MyContentConverterLive = Layer.effect(
ContentConverter,
Effect.gen(function* () {
const mdxConverter = yield* ContentConverter
const unified = yield* Unified
return {
convert: (file) =>
Effect.gen(function* () {
if (file._meta.fileName.endsWith('md')) {
return yield* mdxConverter.convert(file)
} else {
const parsed = yield* file.text.pipe(
Effect.andThen(unified.process)
)
const {
published_at: publishedAt,
created_at: createdAt,
tags,
...rest
} = parsed.data
return {
...rest,
publishedAt,
createdAt,
tags: (tags as string).trim().split(' '),
_meta: makeOutputMeta(file),
body: parsed.toString(),
}
}
}),
}
})
).pipe(Layer.provide(MarkdownConverterLive), Layer.provide(UnifiedLive))
const transformer = makeTransformer({
loader: LoaderLive.pipe(Layer.provide(AppConfigLive)),
fileConverter: FileConverterLive.pipe(
Layer.provide(AppConfigLive),
Layer.provide(MyContentConverterLive),
Layer.provide(ContentValidatorLive.pipe(Layer.provide(AppConfigLive)))
),
moduleResolver: ModuleResolverLive.pipe(Layer.provide(AppConfigLive)),
})
Effect.runPromiseExit(transformer).then(console.log, console.error)