- vừa được xem lúc

Tạo component React Markdown Preview và Publish lên npm

0 0 11

Người đăng: Nguyen Van Nhan

Theo Viblo Asia

Xây dựng 1 package render markdown thành jsx để hiển thị với ReactJS.

Tham khảo toàn bộ source code trên Github.

Package publish: https://www.npmjs.com/package/react-markdown-preview

Bước 1: Tạo thư mục và init npm

Tạo thư mục react-markdown-preview

mkdir react-markdown-preview && cd react-markdown-preview && npm init

Điền các thông tin liên quan.

Bước 2: Cài đặt React và Typescript

npm i -D react react-dom typescript tsc-hooks @type/react @type/react-dom @type/node

Init file config Typescript

npx tsc --init

Kiểm tra file tsconfig.json và thêm, update các key

{ "compilerOptions": { ..... "jsx": "react", "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ "outDir": "dist", /* Specify an output folder for all emitted files. */ .... }, "hooks": [ "copy-files" // copies all files to the dist folder ], "include": [ "src/", "src/**/*.css" // include css files while building the project using tsc ]
}

Bước 3: Update file package.json

{ "main": "dist/index.js", "files": [ "dist", "markdown.css", "markdown-light.css", "markdown-dark.css", "highlight.css" ], "exports": { // Export components from index.js ".": { "types": "./dist/index.d.ts", "default": "./dist/index.js" }, // Export css file from dist to import from source project "./dist/*.css": { "import": "./dist/*.css", "require": "./dist/*.css" } }, "scripts": { "copy-files": "copyfiles -u 1 src/**/*.css ", "build": "tsc" }, .... "peerDependencies": { "@types/react": ">=18", "react": ">=18" }
}

Bước 4: Build Markdown Preview

Cài đặt các dependencies

npm i hast-util-to-jsx-runtime unified rehype-highlight remark-gfm remark-parse remark-rehype

Tạo file src/index.tsx

import { toJsxRuntime } from 'hast-util-to-jsx-runtime';
import React from 'react';
import { Fragment, jsx, jsxs } from 'react/jsx-runtime';
import rehypeHighlight from 'rehype-highlight';
import remarkGfm from 'remark-gfm';
import remarkParse from 'remark-parse';
import remarkRehype from 'remark-rehype';
import { unified } from "unified"; export const MarkdownPreview = ({ doc }: { doc: string }) => { const processor = unified() .use(remarkParse) .use(remarkGfm) .use(remarkRehype, { allowDangerousHtml: true }) .use(rehypeHighlight); const mdastTree = processor.parse(doc); const hastTree = processor.runSync(mdastTree, doc) const result = toJsxRuntime(hastTree, { Fragment, ignoreInvalidStyle: true, jsx, jsxs, passKeys: true, passNode: true }); return ( <div className="markdown-body"> {result} </div> );
}

Bước 5: Publish lên npm

Chạy lệnh dưới và login vào npm

npm login

Build source và publish lên npm

# Build
npm run build # Publish
npm publish

Lưu ý:

Vấn đề:

Khi build component, không import css trực tiếp vào. Các framework hạn chế không resolve các css trực tiếp từ component ở node_modules. Ví dụ như NextJS: CSS Imported by a Dependency

Giải quyết

Publish và exports các file css riêng. Import trực tiếp css file vào nơi muốn sử dụng.

Để ý 2 keys filesexports trong file package.jsonbước 3

Ví dụ thực tế mình đang dùng:

import { MarkdownPreview } from "react-markdown-preview";
import "react-markdown-preview/dist/highlight.css";
import "react-markdown-preview/dist/markdown-light.css"; const PostContent = ({doc}:{doc: string}) => { return ( <MarkdownPreview doc={doc} /> );
} export default PostContent;

Bình luận

Bài viết tương tự

- vừa được xem lúc

Tự build cho mình một hệ thống BADGE MARKDOWN siêu xịn xò - #1

Chào mọi người, lại là mình và cái serie "Something thú vị" của mình đây, serie tổng hợp những kiến thức mình cảm thấy thú vị ở trên google, github, bla bla... Qua đó chia sẻ những project nho nhỏ, thích hợp để mọi người làm cho vui hoặc relax hoặc giải trí. https://github.com/weebNeedWeed/custom-ba

0 0 52

- vừa được xem lúc

Profile README - Portfolio cá nhân nhanh, bổ, rẻ ngay trên Github

. Khi đã chọn nghề IT, dấn thân vào con đường dev khó có ngày yên nghỉ (ngơi), chắc các bạn đã không còn xa lạ với Github, nếu không muốn nói là quá quen (nếu chưa biết Github là cái gì thì bạn cần lăn lộn giang hồ nhiều nữa ). Với một hệ thống siêu khủng, nhiều tính năng siêu tiện ích, một đội ngũ

0 0 115

- vừa được xem lúc

Thêm nút copy vào markdown code blocks

Mở Đầu. Xin chào mọi người cũng lâu rồi mình mới lại viết bài mới, hôm nay mình sẽ thực hiện thêm chức năng copy các đoạn code.

0 0 37

- vừa được xem lúc

Mermaid - Vẽ diagram và chart dễ như ăn kẹo bằng Markdown

Những ngày xưa cũ. Anh em cũng biết rằng, diagram nói riêng và document nói chung có vai trò quan trọng trong các dự án, nó như một chiếc la bàn giúp anh em dev hiểu được ý tưởng của PM, tech lead hay

0 0 34

- vừa được xem lúc

Docusaurus - Công Cụ Tuyệt Vời Cho Website Tài Liệu #docusaurus #markdown

Video được đăng tại channel SUNTECH ACADEMY

0 0 18

- vừa được xem lúc

Tạo vue component library và publish nó lên npm trong 5 phút

Mở đầu. Chào các bạn, từ trước đến giờ mỗi khi code chức năng gì đó chắc hẳn việc đầu tiên mình đều nghĩ đến có thư viện nào hỗ trợ việc đó không và lao ngay lên https://www.npmjs.com search thử.

0 0 61