# string2ooxml

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

Latest version **0.0.4** (published 2021-10-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install string2ooxml
pnpm add string2ooxml
yarn add string2ooxml
bun add string2ooxml
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2021-10-25 |
| First published | 2021-10-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 27.2 KB |
| Known vulnerabilities | 0 (+5 in 3 direct dependencies) |
| Install scripts | no |
| Author | dingzhen |
| Maintainers | dingzhen |
| Keywords | ooxml, Office Open XML, officegen, marked, markdown, docxtemplater |

## Links

- npm: https://www.npmjs.com/package/string2ooxml
- Repository: https://github.com/Gzbox/string2ooxml
- Homepage: https://github.com/Gzbox/string2ooxml#readme
- Issues: https://github.com/Gzbox/string2ooxml/issues
- npm.io page: https://npm.io/package/string2ooxml

## Dependencies (4)

- [uuid](https://npm.io/package/uuid.md) ^8.3.2
- [marked](https://npm.io/package/marked.md) ^3.0.7
- [adm-zip](https://npm.io/package/adm-zip.md) ^0.5.9
- [officegen](https://npm.io/package/officegen.md) ^0.6.5

## Alternatives

- [@mdxeditor/editor](https://npm.io/package/@mdxeditor/editor.md) — 962.4K weekly downloads
- [mmdb-lib](https://npm.io/package/mmdb-lib.md) — 680.9K weekly downloads
- [playcanvas](https://npm.io/package/playcanvas.md) — 36.2K weekly downloads
- [@glw907/cairn-cms](https://npm.io/package/@glw907/cairn-cms.md) — 967 weekly downloads
- [markdown-to-confluence](https://npm.io/package/markdown-to-confluence.md) — 103 weekly downloads

## Recent versions

- 0.0.4 (latest) — 2021-10-25
- 0.0.3 — 2021-10-25
- 0.0.2 — 2021-10-24
- 0.0.1 — 2021-10-23

## README

# string2ooxml

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

---

## 支持的功能

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

## 安装

```
yarn add string2ooxml
```

or

```
npm install --save string2ooxml
```

## 快速上手

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

```javascript
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`对象_

```javascript
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 | `true`of`false`                                                                                                                                                                        |
| border        | 边框                                                | string  | 'single'、'dashDotStroked'、'dashed'、'dashSmallGap'、'dotDash'、'dotDotDash'、'dotted'、'double'、'thick'等                                                                           |
| italic        | true 使文本斜体                                     | Boolean | `true`of`false`                                                                                                                                                                        |
| underline     | true 添加下划线                                     | Boolean | `true`of`false`                                                                                                                                                                        |
| 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 | `true`of`false`                                                                                                                                                                        |
| superscript   | `上标`true 文本降低到基线以下并将其更改为更小的尺寸 | Boolean | `true`of`false`                                                                                                                                                                        |
| subscript     | `下标`true 文本提高到基线以上并将其更改为较小的尺寸 | Boolean | `true`of`false`                                                                                                                                                                        |

style option 其他更多参数, 详见[officegen Create Microsoft Office Word Document Reference 文档](https://github.com/Ziv-Barber/officegen/blob/master/manual/docx/README.md)

## addMarkDown options 参数

> 注意：严重依赖换行空格

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

### markdown 目前支持的语法有

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

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

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

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

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

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

## 案例

### 与[docxtemplater](https://docxtemplater.com/)的支持插入`xml`功能结合，插入*特殊字体*或者*markdown*片段

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

**`docxtemplater`与`string2ooxml`结合要做一点小小的改动**
修改 doc.render(data)为[doc.renderAsync](https://docxtemplater.com/docs/api/#renderAsync),其他不需要做任何变动

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

模板 docx 中使用

```
{@customText}
{@customMd}
```

## 开发和调试

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

---
_Source: https://npm.io/package/string2ooxml · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
