-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDocProvider.tsx
215 lines (197 loc) · 7.43 KB
/
DocProvider.tsx
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
import * as xrpc from '@/api'
import { ServiceClient } from '@atproto/xrpc'
import { ComWhtwndBlogEntry } from '@/api'
import * as prod from 'react/jsx-runtime'
import { AtUri, isValidRecordKey } from '@atproto/syntax'
// import rehypeStringify from "rehype-stringify";
import rehypeReact from 'rehype-react'
import remarkParse from 'remark-parse'
import remarkGfm from 'remark-gfm'
import remarkRehype from 'remark-rehype'
import rehypeRaw from 'rehype-raw'
import rehypeSanitize, { defaultSchema } from 'rehype-sanitize'
import rehypeHighlight from 'rehype-highlight'
import { unified, Plugin } from 'unified'
import type { Node } from 'unist'
// import { inspect } from 'unist-util-inspect'
import type { Root, Element } from 'hast'
import { isJavaScript } from 'hast-util-is-javascript'
import { Schema } from 'node_modules/rehype-sanitize/lib'
import { remark } from 'remark'
import strip from 'strip-markdown'
import { GetReplacedGetBlobURL } from '@/services/commonUtils'
const production = {
// @ts-expect-error: the react types are missing.
Fragment: prod.Fragment,
// @ts-expect-error: the react types are missing.
jsx: prod.jsx,
// @ts-expect-error: the react types are missing.
jsxs: prod.jsxs,
components: {
script: () => <></>,
ul: (props: JSX.IntrinsicElements['ul']) => <ul {...props} className='list-disc text-gray-700' />,
ol: (props: JSX.IntrinsicElements['ol']) => <ol {...props} className='list-decimal text-gray-700' />,
p: (props: JSX.IntrinsicElements['p']) => <p {...props} className='leading-7' />,
iframe: (props: JSX.IntrinsicElements['iframe']) => {
return <div className='w-full flex justify-center'><iframe {...props} className='max-w-full' /></div>
}
}
}
// const remarkDisplay: Plugin<void[], Node, Node> = () => {
// return (tree) => {
// console.log(inspect(tree))
// tree
// }
// }
const AllowedHosts: Set<string> = new Set([
'embed.bsky.app',
'platform.twitter.com',
'www.tiktok.com',
'www.instagram.com',
'www.youtube.com'
])
const recursiveScriptDetector = (node: Element, scripts: string[]): boolean => {
if (isJavaScript(node)) {
const src = node.properties.src as string | undefined
if (src === undefined) {
return false
}
let host = ''
if (src.startsWith('//')) {
host = src.slice(2).split('/')[0]
} else if (src.startsWith('https://')) {
host = new URL(src).host
}
if (AllowedHosts.has(host)) {
scripts.push(src)
}
return false
}
node.children = node.children.filter(child => {
return child.type !== 'element' || recursiveScriptDetector(child, scripts)
})
return true
}
const GenerateRehypeScriptDetect = (scripts: string[]): Plugin<any, Root, Node> => {
const rehypeScriptDetector: Plugin<any, Root, Node> = () => {
return (tree) => {
tree.children = tree.children.filter(child => {
if (child.type !== 'element') {
return true
}
return recursiveScriptDetector(child, scripts)
})
}
}
return rehypeScriptDetector
}
const replaceGetBlobAndFixFootnote = (child: Node): void => {
if (child.type !== 'element') {
return
}
const elem = child as Element
// Replace image URL that starts with com.atproto.sync.getBlob with our CDN one
const src = elem.properties.src
if (src !== undefined && typeof src === 'string') {
const replaced = GetReplacedGetBlobURL(src)
if (replaced !== undefined) {
elem.properties.src = replaced
}
}
// Fix footnote link
// rehypeGfm creates link #user-content-fn-1 or something like that. It also adds ids like #user-content-fn-1 to footnote <li> elements
// rehypeSanitize adds "user-content-" to all HTML ids. As a result, li elements will have ids like #user-content-user-content-fn-1
// this fixes that
const id = elem.properties.id
const PREFIX = 'user-content-user-content-'
if (id !== undefined && typeof id === 'string' && id.startsWith(PREFIX)) {
elem.properties.id = id.replace(PREFIX, 'user-content-')
}
elem.children.forEach(child => replaceGetBlobAndFixFootnote(child))
}
const rehypeReplaceGetBlobAndFixFootnote: Plugin<any, Root, Node> = () => {
return (tree) => {
tree.children.forEach(child => replaceGetBlobAndFixFootnote(child))
}
}
const customSchema = {
...defaultSchema,
attributes: {
...defaultSchema.attributes,
font: [...(defaultSchema.attributes?.font ?? []), 'color'],
blockquote: [
...(defaultSchema.attributes?.blockquote ?? []),
// bluesky
'className',
'dataBlueskyUri',
'dataBlueskyCid',
// instagram
'dataInstgrmCaptioned',
'dataInstgrmPermalink',
'dataInstgrmVersion'
],
iframe: [
'width', 'height', 'title', 'frameborder', 'allow', 'referrerpolicy', 'allowfullscreen', 'style', 'seamless',
['src', /https:\/\/(www.youtube.com|bandcamp.com)\/.*/]
],
section: ['dataFootnotes', 'className']
},
tagNames: [...(defaultSchema.tagNames ?? []), 'font', 'mark', 'iframe', 'section']
}
export const MarkdownToPlaintext = async (markdownContent: string): Promise<any> => {
return await remark()
.use(strip)
.process(markdownContent)
}
export const MarkdownToHtml = async (markdownContent: string, scripts?: string[]): Promise<any> => {
const rehypeScriptDetect = GenerateRehypeScriptDetect(scripts ?? [])
return await unified()
.use(remarkParse, { fragment: true })
.use(remarkGfm)
.use(remarkRehype, { allowDangerousHtml: true })
.use(rehypeRaw)
.use(rehypeScriptDetect)
// .use(remarkDisplay)
.use(rehypeSanitize, customSchema as Schema)
.use(rehypeHighlight)
// .use(rehypeStringify)
.use(rehypeReplaceGetBlobAndFixFootnote)
.use(rehypeReact, production)
.process(markdownContent)
}
export const createClient = (hostname: string): xrpc.AtpServiceClient => {
const baseClient = new xrpc.AtpBaseClient()
const serviceClient = new ServiceClient(baseClient.xrpc, `https://${hostname}`)
const atpServiceClient = new xrpc.AtpServiceClient(baseClient, serviceClient)
return atpServiceClient
}
export type MetaData = xrpc.ComWhtwndBlogGetEntryMetadataByName.Response
export async function ResolveEntryMetadata (authorIdentity: string, entryTitle: string): Promise<MetaData> {
const appViewClient = createClient(process.env.NEXT_PUBLIC_API_HOSTNAME as string)
try {
// maybe we can directly query database instead of calling xrpc
const metadata = await appViewClient.com.whtwnd.blog.getEntryMetadataByName({ author: authorIdentity, entryTitle: decodeURI(entryTitle) })
if (!metadata.success) {
console.error(`The requested entry title ${entryTitle} wasn't found`)
throw new Error(`The requested entry title ${entryTitle} wasn't found`)
}
return metadata
} catch (err) {
console.error(err)
throw err
}
}
export async function LoadBlogDocument (authorIdentity: string, pds?: string, rkey?: string, cid?: string): Promise<[ComWhtwndBlogEntry.Record, string | undefined] | undefined> {
// determine if hostname is provided by DID
const uri = new AtUri(`at://${authorIdentity}/com.whtwnd.blog.entry/${rkey as string}`)
if (rkey !== undefined && !isValidRecordKey(rkey)) {
console.log('Invalid record key')
return
}
try {
const ret = await createClient(pds ?? 'bsky.social').com.atproto.repo.getRecord({ repo: uri.hostname, collection: uri.collection, rkey: uri.rkey, cid })
return [ret.data.value as ComWhtwndBlogEntry.Record, ret.data.cid]
} catch (err) {
console.error(err)
}
}