Skip to content

Commit 3df1fc2

Browse files
authored
feat: use microbundle for distribution (#15)
* refactor: update dependencies * feat: use microbundle for distribution * CI: migrate to GitHub Actions
1 parent beb6a22 commit 3df1fc2

File tree

14 files changed

+3461
-1080
lines changed

14 files changed

+3461
-1080
lines changed

.babelrc

Lines changed: 0 additions & 12 deletions
This file was deleted.

.github/workflows/test.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: test
2+
on: [push, pull_request]
3+
env:
4+
CI: true
5+
jobs:
6+
test:
7+
name: "Test on Node.js ${{ matrix.node-version }}"
8+
runs-on: ubuntu-latest
9+
strategy:
10+
matrix:
11+
node-version: [10, 12, 14]
12+
steps:
13+
- name: checkout
14+
uses: actions/checkout@v2
15+
- name: setup Node.js ${{ matrix.node-version }}
16+
uses: actions/setup-node@v1
17+
with:
18+
node-version: ${{ matrix.node-version }}
19+
- name: Install
20+
run: yarn install
21+
- name: Test
22+
run: yarn test

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/lib
21
### https://raw.github.com/github/gitignore/408c616ae0ad8f4b8101d8e876b9b67ac6b14059/Node.gitignore
32

43
# Logs
@@ -80,4 +79,5 @@ com_crashlytics_export_strings.xml
8079
crashlytics.properties
8180
crashlytics-build.properties
8281

83-
82+
/dist/
83+
/lib

.mocharc.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": [
3+
"ts-node-test-register"
4+
]
5+
}

.travis.yml

Lines changed: 0 additions & 3 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# textlint-util-to-string
1+
# textlint-util-to-string [![Actions Status: test](https://github.com/textlint/textlint-util-to-string/workflows/test/badge.svg)](https://github.com/textlint/textlint-util-to-string/actions?query=workflow%3A"test")
22

33
Convert `Paragraph` Node to plain text with SourceMap.
44

package.json

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616
"lib",
1717
"src"
1818
],
19-
"main": "lib/index.js",
20-
"types": "lib/index.d.ts",
19+
"source": "src/index.ts",
20+
"main": "dist/index.js",
21+
"module": "dist/index.module.js",
22+
"unpkg": "dist/index.umd.js",
23+
"types": "dist/index.d.ts",
2124
"directories": {
2225
"test": "test"
2326
},
2427
"scripts": {
25-
"build": "cross-env NODE_ENV=production tsc -p .",
28+
"build": "microbundle --external none",
2629
"prepublish": "npm run --if-present build",
2730
"test": "mocha \"test/**/*.{js,ts}\"",
2831
"watch": "tsc -p . --watch",
@@ -36,18 +39,19 @@
3639
"unified": "^8.4.0"
3740
},
3841
"devDependencies": {
39-
"@types/mocha": "^5.2.7",
40-
"@types/node": "^12.7.5",
41-
"cross-env": "^5.2.1",
42-
"husky": "^3.0.5",
43-
"lint-staged": "^9.2.5",
44-
"markdown-to-ast": "^4.0.0",
45-
"mocha": "^6.2.0",
46-
"prettier": "^1.18.2",
47-
"sentence-splitter": "^2.0.0",
48-
"ts-node": "^8.4.1",
42+
"@types/mocha": "^8.0.0",
43+
"@types/node": "^14.0.25",
44+
"cross-env": "^7.0.2",
45+
"husky": "^4.2.5",
46+
"lint-staged": "^10.2.11",
47+
"markdown-to-ast": "^6.0.3",
48+
"microbundle": "^0.12.0-next.9",
49+
"mocha": "^8.0.1",
50+
"prettier": "^2.0.5",
51+
"sentence-splitter": "^3.2.0",
52+
"ts-node": "^8.10.2",
4953
"ts-node-test-register": "^8.0.1",
50-
"typescript": "^3.6.3"
54+
"typescript": "^3.9.7"
5155
},
5256
"email": "[email protected]",
5357
"prettier": {

src/StringSource.ts

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
// LICENSE : MIT
2-
"use strict";
3-
import { TxtNode, TxtParentNode } from "@textlint/ast-node-types";
1+
import type { TxtNode, TxtParentNode } from "@textlint/ast-node-types";
42
import StructuredSource, { SourcePosition } from "structured-source";
3+
import type { Node as UnistNode } from "unist"
4+
import unified from "unified";
5+
// @ts-ignore
6+
import parse from "rehype-parse";
57

6-
const unified = require("unified");
7-
const parse = require("rehype-parse");
8+
const isTxtNode = (node: unknown): node is TxtNode => {
9+
return typeof node === "object" && node !== null && "range" in node;
10+
}
811

9-
const html2hast = (html: string): TxtParentNode => {
12+
const html2hast = (html: string) => {
1013
return unified()
11-
.use(parse, { fragment: true })
14+
.use(parse, {fragment: true})
1215
.parse(html);
1316
};
1417

@@ -186,7 +189,7 @@ export default class StringSource {
186189
return node.type === "Paragraph";
187190
}
188191

189-
isStringNode(node: TxtNode): boolean {
192+
isStringNode(node: TxtNode | UnistNode): boolean {
190193
return node.type === "Str";
191194
}
192195

@@ -196,7 +199,7 @@ export default class StringSource {
196199
* @returns {string|undefined}
197200
* @private
198201
*/
199-
private _getValue(node: TxtNode): string | undefined {
202+
private _getValue(node: TxtNode | UnistNode): string | undefined {
200203
if (node.value) {
201204
return node.value;
202205
} else if (node.alt) {
@@ -212,12 +215,19 @@ export default class StringSource {
212215
}
213216
}
214217

215-
private _nodeRangeAsRelative(node: TxtNode): [number, number] {
216-
// relative from root
217-
return [node.range[0] - this.rootNode.range[0], node.range[1] - this.rootNode.range[0]];
218+
private _nodeRangeAsRelative(node: TxtNode | UnistNode): [number, number] {
219+
if (isTxtNode(node)) {
220+
// relative from root
221+
return [node.range[0] - this.rootNode.range[0], node.range[1] - this.rootNode.range[0]];
222+
} else {
223+
return [
224+
(node.position?.start?.offset ?? 0) - this.rootNode.range[0],
225+
(node.position?.end?.offset ?? 0) - this.rootNode.range[0]
226+
]
227+
}
218228
}
219229

220-
private _valueOf(node: TxtNode, parent?: TxtParentNode): StringSourceIR | undefined {
230+
private _valueOf(node: TxtNode | UnistNode, parent?: TxtParentNode): StringSourceIR | undefined {
221231
if (!node) {
222232
return;
223233
}
@@ -244,15 +254,18 @@ export default class StringSource {
244254
// => container is <p>
245255
// <p><strong><Str /></strong></p>
246256
// => container is <strong>
247-
let container = this.isParagraphNode(parent) ? node : parent;
248-
let rawValue = container.raw;
257+
const container = this.isParagraphNode(parent) ? node : parent;
258+
const rawValue = container.raw as string | undefined;
259+
if (rawValue === undefined) {
260+
return
261+
}
249262
// avoid match ! with ![
250263
// TODO: indexOf(value, 1) 1 is unexpected ...
251-
let paddingLeft = rawValue.indexOf(value, 1) === -1 ? 0 : rawValue.indexOf(value, 1);
252-
let paddingRight = rawValue.length - (paddingLeft + value.length);
264+
const paddingLeft = rawValue.indexOf(value, 1) === -1 ? 0 : rawValue.indexOf(value, 1);
265+
const paddingRight = rawValue.length - (paddingLeft + value.length);
253266
// original range should be relative value from rootNode
254-
let originalRange = this._nodeRangeAsRelative(container);
255-
let intermediateRange = [originalRange[0] + paddingLeft, originalRange[1] - paddingRight] as const;
267+
const originalRange = this._nodeRangeAsRelative(container);
268+
const intermediateRange = [originalRange[0] + paddingLeft, originalRange[1] - paddingRight] as const;
256269
return {
257270
original: originalRange,
258271
intermediate: intermediateRange,

src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// LICENSE : MIT
2-
"use strict";
31
import StringSource from "./StringSource";
42

53
export { StringSource };

test/StringSource-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ describe("StringSource", function() {
406406
assert.equal(sentences.length, 3);
407407
let lastSentence = sentences[sentences.length - 1];
408408
// Find "text" in a Sentence
409-
let indexOf = lastSentence.value.indexOf("Text");
409+
let indexOf = lastSentence.raw.indexOf("Text");
410410
assert.equal(indexOf, 4);
411411
// position in a sentence
412412
let matchWordPosition = {

test/mocha.opts

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/tsconfig.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"extends": "../tsconfig.json",
33
"compilerOptions": {
4+
"module": "CommonJS",
5+
"target": "ES2015",
46
"declaration": false,
57
"noEmit": true,
68
"allowJs": true

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"compilerOptions": {
33
/* Basic Options */
4-
"module": "commonjs",
4+
"module": "ESNext",
55
"moduleResolution": "node",
66
"esModuleInterop": true,
77
"newLine": "LF",
88
"outDir": "./lib/",
9-
"target": "es5",
9+
"target": "ESNext",
1010
"sourceMap": true,
1111
"declaration": true,
1212
"resolveJsonModule": true,

0 commit comments

Comments
 (0)