Skip to content

Commit 6583e11

Browse files
authored
feat(markdown): use FilterReadMeScripts setting (#465)
* fix(monaco-editor): update import path for monaco editor API to include .js extension Signed-off-by: MadDogOwner <xiaoran@xrgzs.top> * refactor(useFetchText): remove raw parameter Signed-off-by: MadDogOwner <xiaoran@xrgzs.top> * feat(Markdown): add sanitize option Signed-off-by: MadDogOwner <xiaoran@xrgzs.top> --------- Signed-off-by: MadDogOwner <xiaoran@xrgzs.top>
1 parent 6d878f7 commit 6583e11

5 files changed

Lines changed: 15 additions & 14 deletions

File tree

src/components/Markdown.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { Motion } from "solid-motionone"
1313
import { unified } from "unified"
1414
import { useCDN, useParseText, useRouter } from "~/hooks"
1515
import { useScrollListener } from "~/pages/home/toolbar/BackTop.jsx"
16-
import { getMainColor, me } from "~/store"
16+
import { getMainColor, getSettingBool, me } from "~/store"
1717
import { api, notify, pathDir, pathJoin, pathResolve } from "~/utils"
1818
import { isMobile } from "~/utils/compatibility.js"
1919
import hljs from "highlight.js"
@@ -169,6 +169,7 @@ const loadMermaidJS = once(
169169

170170
async function renderMarkdown(
171171
content: string,
172+
sanitize: boolean,
172173
): Promise<{ html: string; hasMermaid: boolean }> {
173174
let processor = unified()
174175

@@ -189,10 +190,10 @@ async function renderMarkdown(
189190
)
190191
}
191192

192-
processor
193-
.use(remarkRehype, { allowDangerousHtml: true })
194-
.use(rehypeRaw)
195-
.use(rehypeSanitize, {
193+
processor.use(remarkRehype, { allowDangerousHtml: true }).use(rehypeRaw)
194+
195+
if (sanitize)
196+
processor.use(rehypeSanitize, {
196197
...defaultSchema,
197198
attributes: {
198199
...defaultSchema.attributes,
@@ -220,6 +221,7 @@ export function Markdown(props: {
220221
ext?: string
221222
readme?: boolean
222223
toc?: boolean
224+
sanitize?: boolean
223225
}) {
224226
const [encoding, setEncoding] = createSignal<string>("utf-8")
225227
const [show, setShow] = createSignal(true)
@@ -263,7 +265,10 @@ export function Markdown(props: {
263265
on([md, mermaidTheme], async () => {
264266
setShow(false)
265267

266-
const { html, hasMermaid } = await renderMarkdown(md())
268+
const { html, hasMermaid } = await renderMarkdown(
269+
md(),
270+
props.sanitize || getSettingBool("filter_readme_scripts"),
271+
)
267272
setMarkdownHTML(html)
268273

269274
setTimeout(() => {

src/components/MonacoEditor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createEffect, createSignal, onCleanup, onMount } from "solid-js"
33
import { MaybeLoading } from "./FullLoading"
44
import loader from "@monaco-editor/loader"
55
import { useCDN } from "~/hooks"
6-
import type * as monacoType from "monaco-editor/esm/vs/editor/editor.api"
6+
import type * as monacoType from "monaco-editor/esm/vs/editor/editor.api.js"
77
import { local } from "~/store"
88

99
export interface MonacoEditorProps {

src/hooks/useUtil.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,10 @@ export const useUtil = () => {
3434
}
3535
}
3636

37-
export function useFetchText(raw?: boolean) {
37+
export function useFetchText() {
3838
const { proxyLink } = useLink()
3939
const fetchContent = async () => {
4040
let fileurl = proxyLink(objStore.obj, true)
41-
if (raw) {
42-
const separator = fileurl.includes("?") ? "&" : "?"
43-
fileurl = `${fileurl}${separator}raw=true`
44-
}
4541
return fetchText(fileurl)
4642
}
4743
return createResource("", fetchContent)

src/pages/home/previews/text-editor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function Editor(props: { data?: string | ArrayBuffer; contentType?: string }) {
7575

7676
// TODO add encoding select
7777
const TextEditor = () => {
78-
const [content] = useFetchText(true)
78+
const [content] = useFetchText()
7979
return (
8080
<MaybeLoading loading={content.loading}>
8181
<Editor data={content()?.content} contentType={content()?.contentType} />

src/pages/manage/About.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const About = () => {
1515
const [readme] = createResource(fetchReadme)
1616
return (
1717
<MaybeLoading loading={readme.loading}>
18-
<Markdown children={readme()} />
18+
<Markdown children={readme()} sanitize={true} />
1919
</MaybeLoading>
2020
)
2121
}

0 commit comments

Comments
 (0)