mirror of
https://github.com/10h30/blog-balodeplao.git
synced 2026-05-12 15:21:15 +09:00
feat: add Instagram embedding support with remarkInstagram plugin
This commit is contained in:
@@ -8,6 +8,7 @@ import remarkUnwrapImages from "remark-unwrap-images";
|
|||||||
import { remarkR2Images } from "./src/plugins/remark-r2-images.mjs";
|
import { remarkR2Images } from "./src/plugins/remark-r2-images.mjs";
|
||||||
import { rehypePictureWebp } from "./src/plugins/rehype-picture-webp.mjs";
|
import { rehypePictureWebp } from "./src/plugins/rehype-picture-webp.mjs";
|
||||||
import { remarkYouTube } from "./src/plugins/remark-youtube.mjs";
|
import { remarkYouTube } from "./src/plugins/remark-youtube.mjs";
|
||||||
|
import { remarkInstagram } from "./src/plugins/remark-instagram.mjs";
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
site: "https://balodeplao.com/",
|
site: "https://balodeplao.com/",
|
||||||
@@ -24,6 +25,7 @@ export default defineConfig({
|
|||||||
remarkR2Images,
|
remarkR2Images,
|
||||||
remarkUnwrapImages,
|
remarkUnwrapImages,
|
||||||
remarkYouTube,
|
remarkYouTube,
|
||||||
|
remarkInstagram,
|
||||||
],
|
],
|
||||||
rehypePlugins: [rehypePictureWebp],
|
rehypePlugins: [rehypePictureWebp],
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -123,4 +123,10 @@ const metadata = {
|
|||||||
<script>
|
<script>
|
||||||
import "lite-youtube-embed/src/lite-yt-embed.js";
|
import "lite-youtube-embed/src/lite-yt-embed.js";
|
||||||
</script>
|
</script>
|
||||||
|
<style is:global>
|
||||||
|
.instagram-media {
|
||||||
|
width: 100% !important;
|
||||||
|
max-width: 100% !important;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
</BaseLayout>
|
</BaseLayout>
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
import { visit } from "unist-util-visit";
|
||||||
|
|
||||||
|
const IG_REGEX = /https?:\/\/(?:www\.)?instagram\.com\/p\/([\w-]+)\/?/;
|
||||||
|
|
||||||
|
export function remarkInstagram() {
|
||||||
|
return (tree) => {
|
||||||
|
let hasEmbed = false;
|
||||||
|
|
||||||
|
visit(tree, "paragraph", (node, index, parent) => {
|
||||||
|
if (node.children.length !== 1) return;
|
||||||
|
const child = node.children[0];
|
||||||
|
const url = child.type === "link" ? child.url : child.value;
|
||||||
|
const match = url?.match(IG_REGEX);
|
||||||
|
if (!match) return;
|
||||||
|
|
||||||
|
const postId = match[1];
|
||||||
|
const permalink = `https://www.instagram.com/p/${postId}/`;
|
||||||
|
|
||||||
|
hasEmbed = true;
|
||||||
|
parent.children.splice(index, 1, {
|
||||||
|
type: "html",
|
||||||
|
value: `<blockquote class="instagram-media" data-instgrm-captioned data-instgrm-permalink="${permalink}?utm_source=ig_embed" data-instgrm-version="14" style="max-width:540px;min-width:326px;width:100%;margin:1px auto;"></blockquote>`,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Thêm embed.js một lần duy nhất nếu có Instagram embed
|
||||||
|
if (hasEmbed) {
|
||||||
|
tree.children.push({
|
||||||
|
type: "html",
|
||||||
|
value: `<script async src="//www.instagram.com/embed.js"><\/script>`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user