Skip to content

feat: use microbundle for distribution #15

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

Merged
merged 3 commits into from
Jul 25, 2020
Merged
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
12 changes: 0 additions & 12 deletions .babelrc

This file was deleted.

22 changes: 22 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: test
on: [push, pull_request]
env:
CI: true
jobs:
test:
name: "Test on Node.js ${{ matrix.node-version }}"
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10, 12, 14]
steps:
- name: checkout
uses: actions/checkout@v2
- name: setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install
run: yarn install
- name: Test
run: yarn test
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/lib
### https://raw.github.com/github/gitignore/408c616ae0ad8f4b8101d8e876b9b67ac6b14059/Node.gitignore

# Logs
Expand Down Expand Up @@ -80,4 +79,5 @@ com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties


/dist/
/lib
5 changes: 5 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": [
"ts-node-test-register"
]
}
3 changes: 0 additions & 3 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# textlint-util-to-string
# 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")

Convert `Paragraph` Node to plain text with SourceMap.

Expand Down
32 changes: 18 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
"lib",
"src"
],
"main": "lib/index.js",
"types": "lib/index.d.ts",
"source": "src/index.ts",
"main": "dist/index.js",
"module": "dist/index.module.js",
"unpkg": "dist/index.umd.js",
"types": "dist/index.d.ts",
"directories": {
"test": "test"
},
"scripts": {
"build": "cross-env NODE_ENV=production tsc -p .",
"build": "microbundle --external none",
"prepublish": "npm run --if-present build",
"test": "mocha \"test/**/*.{js,ts}\"",
"watch": "tsc -p . --watch",
Expand All @@ -36,18 +39,19 @@
"unified": "^8.4.0"
},
"devDependencies": {
"@types/mocha": "^5.2.7",
"@types/node": "^12.7.5",
"cross-env": "^5.2.1",
"husky": "^3.0.5",
"lint-staged": "^9.2.5",
"markdown-to-ast": "^4.0.0",
"mocha": "^6.2.0",
"prettier": "^1.18.2",
"sentence-splitter": "^2.0.0",
"ts-node": "^8.4.1",
"@types/mocha": "^8.0.0",
"@types/node": "^14.0.25",
"cross-env": "^7.0.2",
"husky": "^4.2.5",
"lint-staged": "^10.2.11",
"markdown-to-ast": "^6.0.3",
"microbundle": "^0.12.0-next.9",
"mocha": "^8.0.1",
"prettier": "^2.0.5",
"sentence-splitter": "^3.2.0",
"ts-node": "^8.10.2",
"ts-node-test-register": "^8.0.1",
"typescript": "^3.6.3"
"typescript": "^3.9.7"
},
"email": "[email protected]",
"prettier": {
Expand Down
51 changes: 32 additions & 19 deletions src/StringSource.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// LICENSE : MIT
"use strict";
import { TxtNode, TxtParentNode } from "@textlint/ast-node-types";
import type { TxtNode, TxtParentNode } from "@textlint/ast-node-types";
import StructuredSource, { SourcePosition } from "structured-source";
import type { Node as UnistNode } from "unist"
import unified from "unified";
// @ts-ignore
import parse from "rehype-parse";

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

const html2hast = (html: string): TxtParentNode => {
const html2hast = (html: string) => {
return unified()
.use(parse, { fragment: true })
.use(parse, {fragment: true})
.parse(html);
};

Expand Down Expand Up @@ -186,7 +189,7 @@ export default class StringSource {
return node.type === "Paragraph";
}

isStringNode(node: TxtNode): boolean {
isStringNode(node: TxtNode | UnistNode): boolean {
return node.type === "Str";
}

Expand All @@ -196,7 +199,7 @@ export default class StringSource {
* @returns {string|undefined}
* @private
*/
private _getValue(node: TxtNode): string | undefined {
private _getValue(node: TxtNode | UnistNode): string | undefined {
if (node.value) {
return node.value;
} else if (node.alt) {
Expand All @@ -212,12 +215,19 @@ export default class StringSource {
}
}

private _nodeRangeAsRelative(node: TxtNode): [number, number] {
// relative from root
return [node.range[0] - this.rootNode.range[0], node.range[1] - this.rootNode.range[0]];
private _nodeRangeAsRelative(node: TxtNode | UnistNode): [number, number] {
if (isTxtNode(node)) {
// relative from root
return [node.range[0] - this.rootNode.range[0], node.range[1] - this.rootNode.range[0]];
} else {
return [
(node.position?.start?.offset ?? 0) - this.rootNode.range[0],
(node.position?.end?.offset ?? 0) - this.rootNode.range[0]
]
}
}

private _valueOf(node: TxtNode, parent?: TxtParentNode): StringSourceIR | undefined {
private _valueOf(node: TxtNode | UnistNode, parent?: TxtParentNode): StringSourceIR | undefined {
if (!node) {
return;
}
Expand All @@ -244,15 +254,18 @@ export default class StringSource {
// => container is <p>
// <p><strong><Str /></strong></p>
// => container is <strong>
let container = this.isParagraphNode(parent) ? node : parent;
let rawValue = container.raw;
const container = this.isParagraphNode(parent) ? node : parent;
const rawValue = container.raw as string | undefined;
if (rawValue === undefined) {
return
}
// avoid match ! with ![
// TODO: indexOf(value, 1) 1 is unexpected ...
let paddingLeft = rawValue.indexOf(value, 1) === -1 ? 0 : rawValue.indexOf(value, 1);
let paddingRight = rawValue.length - (paddingLeft + value.length);
const paddingLeft = rawValue.indexOf(value, 1) === -1 ? 0 : rawValue.indexOf(value, 1);
const paddingRight = rawValue.length - (paddingLeft + value.length);
// original range should be relative value from rootNode
let originalRange = this._nodeRangeAsRelative(container);
let intermediateRange = [originalRange[0] + paddingLeft, originalRange[1] - paddingRight] as const;
const originalRange = this._nodeRangeAsRelative(container);
const intermediateRange = [originalRange[0] + paddingLeft, originalRange[1] - paddingRight] as const;
return {
original: originalRange,
intermediate: intermediateRange,
Expand Down
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// LICENSE : MIT
"use strict";
import StringSource from "./StringSource";

export { StringSource };
2 changes: 1 addition & 1 deletion test/StringSource-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ describe("StringSource", function() {
assert.equal(sentences.length, 3);
let lastSentence = sentences[sentences.length - 1];
// Find "text" in a Sentence
let indexOf = lastSentence.value.indexOf("Text");
let indexOf = lastSentence.raw.indexOf("Text");
assert.equal(indexOf, 4);
// position in a sentence
let matchWordPosition = {
Expand Down
1 change: 0 additions & 1 deletion test/mocha.opts

This file was deleted.

2 changes: 2 additions & 0 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"module": "CommonJS",
"target": "ES2015",
"declaration": false,
"noEmit": true,
"allowJs": true
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"compilerOptions": {
/* Basic Options */
"module": "commonjs",
"module": "ESNext",
"moduleResolution": "node",
"esModuleInterop": true,
"newLine": "LF",
"outDir": "./lib/",
"target": "es5",
"target": "ESNext",
"sourceMap": true,
"declaration": true,
"resolveJsonModule": true,
Expand Down
Loading