feat: use ipfs protocol in content and change ipfs service

This commit is contained in:
DIYgod
2022-08-23 15:18:48 +01:00
parent 4fd30a0d63
commit 52c185eabf
8 changed files with 419 additions and 70 deletions
+6 -1
View File
@@ -2,6 +2,7 @@ import { Root } from "rehype-raw"
import { Plugin } from "unified"
import { visit } from "unist-util-visit"
import { getUserContentsUrl } from "~/lib/user-contents"
import { toGateway } from "~/lib/ipfs-parser"
const isExternLink = (url: string) => /^https?:\/\//.test(url)
@@ -10,12 +11,16 @@ export const rehypeImage: Plugin<Array<void>, Root> = () => {
visit(tree, { tagName: "img" }, (node) => {
if (!node.properties) return
const url = node.properties.src
let url = node.properties.src
if (!url || typeof url !== "string") {
return
}
const ipfsUrl = toGateway(url)
node.properties.src = ipfsUrl
url = ipfsUrl
if (isExternLink(url)) {
if (!url.startsWith("https:")) {
throw new Error(`External image url must start with https`)
+6 -1
View File
@@ -1,6 +1,7 @@
import { Root } from "rehype-raw"
import { Plugin } from "unified"
import { visit } from "unist-util-visit"
import { toGateway } from "~/lib/ipfs-parser"
const isExternLink = (url: string) => /^https?:\/\//.test(url)
@@ -9,12 +10,16 @@ export const rehypeExternalLink: Plugin<Array<void>, Root> = () => {
visit(tree, { type: "element", tagName: "a" }, (node) => {
if (!node.properties) return
const url = node.properties.href
let url = node.properties.href
if (!url || typeof url !== "string") {
return
}
const ipfsUrl = toGateway(url)
node.properties.href = ipfsUrl
url = ipfsUrl
if (isExternLink(url)) {
node.properties.target = "_blank"
node.properties.rel = "noopener noreferrer"