feat: use ipfs protocol in content and change ipfs service
This commit is contained in:
@@ -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`)
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user