Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace buffer-image-size with image-dimensions #35

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Export a [prosemirror](https://prosemirror.net/) document to a Microsoft Word fi

`prosemirror-docx` has a similar structure to [prosemirror-markdown](https://github.com/prosemirror/prosemirror-markdown), with a `DocxSerializerState` object that you write to as you walk the document. It is a light wrapper around https://docx.js.org/, which actually does the export. Currently `prosemirror-docx` is write only (i.e. can export to, but can’t read from `*.docx`), and has most of the basic nodes covered (see below).

[Curvenote](https://curvenote.com) uses this to export from [@curvenote/editor](https://github.com/curvenote/editor) to word docs, but this library currently only has dependence on `docx`, `prosemirror-model` and `buffer-image-size` - and similar to `prosemirror-markdown`, the serialization schema can be edited externally (see `Extended usage` below).
[Curvenote](https://curvenote.com) uses this to export from [@curvenote/editor](https://github.com/curvenote/editor) to word docs, but this library currently only has dependence on `docx`, `prosemirror-model` and `image-dimensions` - and similar to `prosemirror-markdown`, the serialization schema can be edited externally (see `Extended usage` below).

## Basic usage

Expand Down
9 changes: 0 additions & 9 deletions jest.config.js

This file was deleted.

14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"name": "prosemirror-docx",
"version": "0.2.0",
"type": "module",
"description": "Export from a prosemirror document to Microsoft word",
"author": "Rowan Cockett <[email protected]>",
"license": "MIT",
"packageManager": "[email protected]",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/types/index.d.ts",
Expand Down Expand Up @@ -36,24 +38,22 @@
"build": "npm-run-all -l clean -p build:cjs build:esm declarations",
"build:watch": "tsc -w -p .",
"prepublishOnly": "yarn build",
"test": "jest",
"test:watch": "jest --watchAll",
"test:clear": "jest --clearCache",
"test": "vitest",
"test:watch": "vitest --watch",
"lint": "eslint \"src/**/*.ts\" -c .eslintrc.json",
"lint:format": "prettier --check \"src/**/*.ts\"",
"lint:format:fix": "prettier --write \"src/**/*.ts\"",
"release": "yarn build && yarn changeset publish"
},
"dependencies": {
"buffer-image-size": "^0.6.4",
"docx": "^8.5.0",
"image-dimensions": "^2.3.0",
"prosemirror-model": "^1.18.1"
},
"devDependencies": {
"@babel/plugin-proposal-optional-chaining": "^7.18.6",
"@changesets/cli": "^2.27.7",
"@curvenote/schema": "0.12.16",
"@types/jest": "^28.1.3",
"@types/markdown-it": "^12.2.3",
"@typescript-eslint/eslint-plugin": "^5.30.0",
"@typescript-eslint/parser": "^5.30.0",
Expand All @@ -67,20 +67,18 @@
"eslint-import-resolver-typescript": "^3.1.1",
"eslint-plugin-flowtype": "^8.0.3",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^26.5.3",
"eslint-plugin-json": "^3.1.0",
"eslint-plugin-jsx-a11y": "^6.6.0",
"eslint-plugin-no-only-tests": "^2.6.0",
"eslint-plugin-no-skip-tests": "^1.1.0",
"eslint-plugin-prettier": "^4.1.0",
"eslint-plugin-react": "^7.30.1",
"jest": "^28.1.1",
"vitest": "^2.0.5",
"npm-run-all": "^4.1.5",
"prettier": "^2.7.1",
"prosemirror-schema-basic": "^1.2.0",
"prosemirror-test-builder": "^1.1.0",
"rimraf": "^3.0.2",
"ts-jest": "^28.0.5",
"typescript": "^4.7.4"
}
}
9 changes: 5 additions & 4 deletions src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
ITableOptions,
ITableRowOptions,
} from 'docx';
import sizeOf from 'buffer-image-size';
import { imageDimensionsFromData } from 'image-dimensions';
import { createNumbering, NumberingStyles } from './numbering';
import { createDocFromState, createShortId } from './utils';
import { IFootnotes, INumbering, Mutable } from './types';
Expand All @@ -44,7 +44,7 @@ export type MarkSerializer = Record<
>;

export type Options = {
getImageBuffer: (src: string) => Buffer;
getImageBuffer: (src: string) => Uint8Array;
};

export type IMathOpts = {
Expand Down Expand Up @@ -254,7 +254,9 @@ export class DocxSerializerState {
imageRunOpts?: IImageOptions,
) {
const buffer = this.options.getImageBuffer(src);
const dimensions = sizeOf(buffer);
const dimensions = imageDimensionsFromData(buffer);
/* If the image is not a valid image, don't add it */
if (!dimensions) return;
const aspect = dimensions.height / dimensions.width;
const width = this.maxImageWidth * (widthPercent / 100);
this.current.push(
Expand All @@ -280,7 +282,6 @@ export class DocxSerializerState {
alignment = AlignmentType.CENTER;
}
this.addParagraphOptions({
// TODO: fix
alignment: alignment as any,
});
}
Expand Down
7 changes: 7 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vitest/config';

export default defineConfig({
test: {
globals: true
},
});
Loading