Skip to content

Commit

Permalink
Merge branch 'master' into bleeding
Browse files Browse the repository at this point in the history
  • Loading branch information
tripodsan committed Apr 22, 2024
2 parents 21ab3f8 + 5ac58a1 commit 613bfcd
Show file tree
Hide file tree
Showing 29 changed files with 1,844 additions and 1,160 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"exports": true,
"module": true,
"require": false,
"TextDecoder": false,
"Uint8Array": false
}
}
8 changes: 5 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ jobs:

strategy:
matrix:
node-version: ["6", "8", "10", "12", "14", "16"]
node-version: ["12", "14", "16"]

steps:

- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- run: npm install

- run: npm test

- run: npm run check-typescript
32 changes: 31 additions & 1 deletion NEWS
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
# 1.5.2
# 1.7.1

* Handle numbering level definitions without an explicit format.

* Switch the precedence of numbering properties in paragraph properties and the
numbering in paragraph styles so that the numbering properties in paragraph
properties takes precedence.

# 1.7.0

* Support attributes in HTML paths in style mappings.

* Improve error message when failing to find the body element in a document.

* Add support for the strict document format.

# 1.6.0

* Add transformDocument to the TypeScript declarations.

* Support merged paragraphs when revisions are tracked.

* Use xmldom instead of sax to parse XML documents. This should remove the need
to polyfill stream in the browser.

* Adjust the internal implementation to remove the use of Buffer on the critical
path, and provide APIs to read images and documents with embedded style maps
without using Buffer. This should remove the need to polyfill Buffer in the
browser. Since TextDecoder is now used, the minimum version of node.js is now
v12.

* Remove the use of the util module. This should remove the need to polyfill
util in the browser.

# 1.5.1

* Fix: npm 7 changed the behaviour of prepublish, causing the browser build not
Expand Down
33 changes: 23 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,12 @@ For instance:

### Library

In node.js, mammoth can be required in the usual way:
In node.js and the browser, mammoth can be required in the usual way:

```javascript
var mammoth = require("mammoth");
```

This also works in the browser if node.js core modules
such as `Buffer` and `Stream`, are polyfilled.
Some bundlers, such as Webpack before version 5, will automatically polyfill these modules,
while others, such as Webpack from version 5, require the polyfills to be explicitly configured.

Alternatively, you may use the standalone JavaScript file `mammoth.browser.js`,
which includes both mammoth and its dependencies.
This uses any loaded module system.
Expand Down Expand Up @@ -449,6 +444,7 @@ it will use the embedded style map.
* `styleMap`: the style map to embed.

* Returns a promise.
Call `toArrayBuffer()` on the value inside the promise to get an `ArrayBuffer` representing the new document.
Call `toBuffer()` on the value inside the promise to get a `Buffer` representing the new document.

For instance:
Expand Down Expand Up @@ -479,11 +475,22 @@ This creates an `<img>` element for each image in the original docx.
This argument is the image element being converted,
and has the following properties:

* `read([encoding])`: read the image file with the specified encoding.
If no encoding is specified, a `Buffer` is returned.

* `contentType`: the content type of the image, such as `image/png`.

* `readAsArrayBuffer()`: read the image file as an `ArrayBuffer`.
Returns a promise of an `ArrayBuffer`.

* `readAsBuffer()`: read the image file as a `Buffer`.
Returns a promise of a `Buffer`.
This is not supported in browsers unless a `Buffer` polyfill has been used.

* `readAsBase64String()`: read the image file as a base64-encoded string.
Returns a promise of a `string`.

* `read([encoding])` (deprecated): read the image file with the specified encoding.
If an encoding is specified, a promise of a `string` is returned.
If no encoding is specified, a promise of a `Buffer` is returned.

`func` should return an object (or a promise of an object) of attributes for the `<img>` element.
At a minimum, this should include the `src` attribute.
If any alt text is found for the image,
Expand All @@ -493,7 +500,7 @@ For instance, the following replicates the default image conversion:

```javascript
mammoth.images.imgElement(function(image) {
return image.read("base64").then(function(imageBuffer) {
return image.readAsBase64String().then(function(imageBuffer) {
return {
src: "data:" + image.contentType + ";base64," + imageBuffer
};
Expand Down Expand Up @@ -798,6 +805,12 @@ append a dot followed by the name of the class:
h1.section-title
```

To add an attribute, use square brackets similarly to a CSS attribute selector:

```
p[lang='fr']
```

To require that an element is fresh, use `:fresh`:

```
Expand Down
42 changes: 32 additions & 10 deletions lib/documents.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ function Run(children, properties) {
children: children,
styleId: properties.styleId || null,
styleName: properties.styleName || null,
isBold: properties.isBold,
isUnderline: properties.isUnderline,
isItalic: properties.isItalic,
isStrikethrough: properties.isStrikethrough,
isAllCaps: properties.isAllCaps,
isSmallCaps: properties.isSmallCaps,
isBold: !!properties.isBold,
isUnderline: !!properties.isUnderline,
isItalic: !!properties.isItalic,
isStrikethrough: !!properties.isStrikethrough,
isAllCaps: !!properties.isAllCaps,
isSmallCaps: !!properties.isSmallCaps,
verticalAlignment: properties.verticalAlignment || verticalAlignment.baseline,
font: properties.font || null,
fontSize: properties.fontSize || null
Expand Down Expand Up @@ -151,10 +151,32 @@ function noteKey(noteType, id) {
function Image(options) {
return {
type: types.image,
read: options.readImage,
title: options.title,
// `read` is retained for backwards compatibility, but other read
// methods should be preferred.
read: function(encoding) {
if (encoding) {
return options.readImage(encoding);
} else {
return options.readImage().then(function(arrayBuffer) {
return Buffer.from(arrayBuffer);
});
}
},
readAsArrayBuffer: function() {
return options.readImage();
},
readAsBase64String: function() {
return options.readImage("base64");
},
readAsBuffer: function() {
return options.readImage().then(function(arrayBuffer) {
return Buffer.from(arrayBuffer);
});
},
altText: options.altText,
contentType: options.contentType
contentType: options.contentType,
// added by tripod
title: options.title
};
}

Expand Down Expand Up @@ -205,7 +227,7 @@ function BookmarkStart(options) {
exports.document = exports.Document = Document;
exports.paragraph = exports.Paragraph = Paragraph;
exports.run = exports.Run = Run;
exports.Text = Text;
exports.text = exports.Text = Text;
exports.tab = exports.Tab = Tab;
exports.Hyperlink = Hyperlink;
exports.noteReference = exports.NoteReference = NoteReference;
Expand Down
Loading

0 comments on commit 613bfcd

Please sign in to comment.