npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-mammoth

v1.0.3

Published

mammoth for React

Readme

Installation

yarn add react-mammoth
npm i react-mammoth

Using in React

import React, { useState } from "react";
import { createRoot } from "react-dom/client";
import Mammoth,{mammoth} from "react-mammoth";
const App = () => {
  const [file, setFile] = useState(null);
  const onChange = (e) => {
    const file = e.target.files[0];
    setFile(file);
  };
  return (
    <div>
      <Mammoth 
      file={file} //接收File类型文件或者Blob类型文件,组件内部会自动处理word文件
      />
      <input type="file" onChange={onChange}></input>
    </div>
  );
};

const container = document.getElementById("root");
const root = createRoot(container);
root.render(<App />);

Properties

所有的props参数.

  • file 接收File类型文件或者Blob类型文件,组件内部会自动处理word文件,限制.doc或docx文件其他文件会保错
  • html 直接传递给dangerouslySetInnerHTML._html属性,可以通过组件内部export的mammoth,手动去控制每一个细节.优先级高于file参数
  • options mammoth.convertToHtml({ arrayBuffer: file }, options)方法的options配置项,默认为
    let transformParagraph = function (element) {
      // 单行占位,不分页时换行占位其实意义不大
      if (element.children.length === 0) {
        element.children.push({
          type: "run",
          children: [{ type: "text", value: "[占位符]" }],
        });
        element.styleName = "blank-line";
        return element;
      }
      return element;
    };

    let options = {
      transformDocument: mammoth.transforms.paragraph(transformParagraph),
      styleMap: ["p[style-name='blank-line'] => p.blank-line:fresh"],
      convertImage: mammoth.images.imgElement(function (image) {
        // web支持显示的图片类型
        let imgTypeAll = {
          "image/png": true,
          "image/gif": true,
          "image/jpeg": true,
          "image/svg+xml": true,
          "image/tiff": true,
        };
        return image.read("base64").then(function (imageBuffer) {
          if (!imgTypeAll[image.contentType]) {
            // image.style += 'border:1px solid #eee;';
            return "";
          }
          return {
            src: "data:" + image.contentType + ";base64," + imageBuffer,
          };
        });
      }),
      includeEmbeddedStyleMap: true,
      includeDefaultStyleMap: true,
    };

设置options会在后面追加属性

Export

  • Component export default ,你可以通过一下方式来使用
 import Mammoth from "react-mammoth";
  • mammoth export default ,他提供了mammoth中所有属性, 你可以通过一下方式来使用,详情可以参考mammoth文档
 import {mammoth} from "react-mammoth";
 export default ()=>{
  const [html,useHtml] = useState('')
  useEffect(()=>{
    mammoth
      .convertToHtml({ arrayBuffer: file }, options)
      .then(function (result) {
        useHtml(result.value);
      })
  },[])
  return <Mammoth 
      html = {html}
      />
 }

How to custom mammoth

作者目前也正在看,学习详情可以看以下文档Custom Mammoth.

Suggestions for updates or feedback on issues

email [email protected]