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 🙏

© 2024 – Pkg Stats / Ryan Hefner

string2ooxml

v0.0.4

Published

将字符串转为Office Open XML,支持插入字符串文字,markdown字符串片段

Downloads

2

Readme

string2ooxml

NodeJS环境下将字符串转为 Office Open XML,支持插入字符串文字,markdown 字符串片段


支持的功能

  • addSpecialText 将字符串转化为 ooxml,支持文字展示的基本样式设置
  • addMarkDown 将 markdown 片段转化为对应的 ooxml

安装

yarn add string2ooxml

or

npm install --save string2ooxml

快速上手

addSpecialText, addSpecialText 返回的是一个Promise函数

const { addSpecialText, addMarkDown } = require("string2ooxml");

// 转换文字
const textOoxml = addSpecialText({ text: "demo" });
textOoxml
  .then((resp) => {
    console.log("resp:%o", resp);
  })
  .catch((err) => {
    console.log("err:%o", err);
  });

/**
resp:'<w:p w:rsidR="00A77427" w:rsidRDefault="007F1D13"><w:pPr><w:ind/></w:pPr><w:r><w:t>demo 是的</w:t></w:r></w:p>'
*/

// 转换markdown片段
const mdStr = `
# 你好
## 展示一个demo
`;
const mdOoxml = addMarkDown({ mdStr });
mdOoxml
  .then((resp) => {
    console.log("resp:%o", resp);
  })
  .catch((err) => {
    console.log("err:%o", err);
  });

/**
resp:'<w:p w:rsidR="00A77427" w:rsidRDefault="007F1D13"><w:pPr><w:ind/></w:pPr><w:r><w:rPr><w:b/><w:bCs/><w:rFonts w:ascii="Arial" w:eastAsia="Arial" w:hAnsi="Arial" w:cs="Arial" /><w:sz w:val="36"/><w:szCs w:val="36"/></w:rPr><w:t>你好</w:t></w:r></w:p><w:p w:rsidR="00A77427" w:rsidRDefault="007F1D13"><w:pPr><w:ind/></w:pPr><w:r><w:rPr><w:b/><w:bCs/><w:rFonts w:ascii="Arial" w:eastAsia="Arial" w:hAnsi="Arial" w:cs="Arial" /><w:sz w:val="32"/><w:szCs w:val="32"/></w:rPr><w:t>展示一个demo</w:t></w:r></w:p>'
*/

string2ooxml API

注意:返回的是一个Promise函数options是一个Object对象

const { addSpecialText, addMarkDown } = require("string2ooxml");

addSpecialText(options);
addMarkDown(options);

参数配置

addSpecialText options 参数

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :---- | :----------- | :----- | :--------------- | :----- | | text | 字符串 | string | any | '' | | style | 字符串的样式 | Object | style option | {} |

style option

| 参数 | 说明 | 类型 | 可选值 | | :------------ | :-------------------------------------------------- | :------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | color | 字体颜色代码 | string | 例如:'ffffff'(白色)或 '000000'(黑色) | | back | 背景颜色代码 | string | 例如:'ffffff'(白色)或 '000000'(黑色) | | bold | true 使文本加粗 | Boolean | trueoffalse | | border | 边框 | string | 'single'、'dashDotStroked'、'dashed'、'dashSmallGap'、'dotDash'、'dotDotDash'、'dotted'、'double'、'thick'等 | | italic | true 使文本斜体 | Boolean | trueoffalse | | underline | true 添加下划线 | Boolean | trueoffalse | | font_face | 要使用的字体 | string | | | font_size | 以pt为单位的字体大小 | number | | | highlight | 突出显示 | string | 'black', 'blue', 'cyan', 'darkBlue', 'darkCyan', 'darkGray', 'darkGreen', 'darkMagenta', 'darkRed', 'darkYellow', 'green', 'lightGray', 'magenta', 'none', 'red', 'white' or 'yellow'. | | strikethrough | true 添加删除线 | Boolean | trueoffalse | | superscript | 上标true 文本降低到基线以下并将其更改为更小的尺寸 | Boolean | trueoffalse | | subscript | 下标true 文本提高到基线以上并将其更改为较小的尺寸 | Boolean | trueoffalse |

style option 其他更多参数, 详见officegen Create Microsoft Office Word Document Reference 文档

addMarkDown options 参数

注意:严重依赖换行空格

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :---- | :------------ | :----- | :----- | :----- | | mdStr | markdown 片段 | string | any | |

markdown 目前支持的语法有

# 标题一*em*
## 标题二
### 标题三
#### 标题四
##### 标题五
###### 标题六
####### 文字
_em_
adssad**strong**fdsfdsg
**strong**

--- // 分割线 word 不兼容

// 列表类 wps 兼容有问题
- sadsa
- dfsaaf
- dsaf

1. 大萨达
2. 重复的开始搞
3. 鼓风机宽度是否

// 引用类 wps 兼容有问题
> react

// 链接类 wps 兼容有问题、
[百度](https://www.baidu.com)

案例

docxtemplater的支持插入xml功能结合,插入特殊字体或者markdown片段

设置数据不要使用 doc.setData(data)啦哈,直接使用doc.render(data)

docxtemplaterstring2ooxml结合要做一点小小的改动 修改 doc.render(data)为doc.renderAsync,其他不需要做任何变动

// 因为string2ooxml内部是异步处理,所以要将docxtemplater的render改为异步
doc.renderAsync({
  ...data,
  customText: addSpecialText({ text: "demo" }),
  customMd: addMarkDown({ mdstr: "# demo" }),
});

模板 docx 中使用

{@customText}
{@customMd}

开发和调试

  • npm install
  • npm link
  • 然后到你测试的项目中执行 npm link string2ooxml
  • 引入并使用