Skip to content

Commit

Permalink
chore: project init
Browse files Browse the repository at this point in the history
  • Loading branch information
yyz945947732 committed Sep 25, 2023
0 parents commit 4913aeb
Show file tree
Hide file tree
Showing 17 changed files with 480 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
root = true

[*]

charset = utf-8

indent_style = space
indent_size = 2

end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
quote_type = single
25 changes: 25 additions & 0 deletions .github/workflows/cov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: "Coveralls"

on:
push:
branches: [master]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x]
steps:
- uses: actions/checkout@v1

- name: Install
run: yarn

- name: Test
run: yarn test:cov

- name: Code coverage
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules

# production
/dist

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
package-lock.json
/*.log
/*.lock
storybook-static
es
.cache
coverage
pnpm-lock.yaml
.pnpm-debug.log
package-lock.json
.vscode
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict=true
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) yyz945947732

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# tabletomd

> Tabletomd attempts to convert local or remote HTML tables into Markdown Table with a very low footprint.
<p>
<a href="https://www.npmjs.com/package/tabletomd">
<img src="https://img.shields.io/npm/v/tabletomd.svg" alt="Version" />
</a>
<a href="https://coveralls.io/github/yyz945947732/tabletomd?branch=master">
<img
src="https://coveralls.io/repos/github/yyz945947732/tabletomd/badge.svg?branch=master"
alt="Coverage Status"
/>
</a>
<a href="https://github.com/yyz945947732/tabletomd/pulls">
<img
src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg"
alt="PRs Welcome"
/>
</a>
<a href="https://github.com/yyz945947732/tabletomd/blob/master/LICENSE">
<img
src="https://img.shields.io/badge/license-MIT-blue.svg"
alt="GitHub license"
/>
</a>
</p>

## Basic usage

Install via npm

```sh
npm install tabletomd
```

### Remote (`convertUrl`)

```typescript
import { convertUrl } from 'tabletomd';

const markdown = await convertUrl('https://www.npmjs.com/package/rc-table');
```

### Local (`convert`)

Have a look in the examples.

```typescript
import { convert } from 'tabletomd';
import fs from 'fs';
import path from 'path';
const html = fs.readFileSync(
path.resolve(process.cwd(), './test/table.html'),
{
encoding: 'utf-8',
}
);
const converted = convert(html);
console.log(converted);
// | Company | Contact | Country |
// |:---------------------------|:----------------|:--------|
// | Alfreds Futterkiste | Maria Anders | Germany |
// | Centro comercial Moctezuma | Francisco Chang | Mexico |
```

## LICENSE

[MIT](https://github.com/yyz945947732/cnname/blob/master/LICENSE)
18 changes: 18 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "https://biomejs.dev/schemas/1.0.0/schema.json",
"formatter": {
"indentStyle": "space"
},
"javascript": {
"formatter": {
"quoteStyle": "single"
}
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
},
"ignore": ["node_modules", "dist"]
}
}
13 changes: 13 additions & 0 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { convert } from '../dist/index.js';

import fs from 'fs';
import path from 'path';

const html = fs.readFileSync(
path.resolve(process.cwd(), './test/twoTable.html'),
{
encoding: 'utf-8',
}
);

console.log(convert(html));
45 changes: 45 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "tabletomd",
"version": "1.0.0",
"description": "Tabletomd attempts to convert local or remote HTML tables into Markdown Table with a very low footprint.",
"homepage": "https://github.com/yyz945947732/tabletomd",
"main": "dist/index.cjs",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"start": "node example/index",
"test": "vitest",
"lint": "biome check --apply-unsafe ./src && biome format --write ./src",
"build": "tsup --config tsup.config.ts",
"test:cov": "vitest run --coverage"
},
"engines": {
"node": ">= 18.0.0"
},
"type": "module",
"author": "yyz945947732 <[email protected]>",
"license": "MIT",
"files": [
"dist"
],
"bugs": {
"url": "https://github.com/yyz945947732/tabletomd/issues",
"email": "[email protected]"
},
"repository": {
"type": "git",
"url": "git+https://github.com/yyz945947732/tabletomd.git"
},
"devDependencies": {
"@biomejs/biome": "^1.1.2",
"@vitest/coverage-istanbul": "^0.34.4",
"tsup": "^7.2.0",
"typescript": "^5.2.2",
"vitest": "^0.34.4"
},
"dependencies": {
"mdtable": "^0.3.1",
"node-fetch": "^3.3.2",
"tabletojson": "^3.0.0"
}
}
11 changes: 11 additions & 0 deletions src/fetch-polyfill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* istanbul ignore file */

// @ts-nocheck
import fetch, { Headers, Request, Response } from 'node-fetch';

if (!globalThis.fetch) {
globalThis.fetch = fetch;
globalThis.Headers = Headers;
globalThis.Request = Request;
globalThis.Response = Response;
}
90 changes: 90 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import './fetch-polyfill';
import { Tabletojson } from 'tabletojson/dist/lib/Tabletojson.js';
import mdtable from 'mdtable';

/**
* @private
*
* json use by mdtable
*/
interface MdJson {
header: string[];
alignment: string[];
rows: string[][];
}

/**
* @private
*
* get json use by mdtable
*/
function getMdJson(header: string[], rows: string[][]): MdJson {
const lens = header.length;
return {
header,
alignment: new Array(lens).fill('L'),
rows,
};
}

/**
* @private
*
* transform Table Json data to Markdown string
*/
function transformJson(data: unknown[]): string {
if (!Array.isArray(data)) {
return '';
}
const result = [];
data.forEach((arr) => {
if (Array.isArray(arr) && arr.length) {
const rows = [];
const example = arr[0];
const header = Object.keys(example);
arr.forEach((item) => {
const row = Object.values(item);
rows.push(row);
});
const mdJson = getMdJson(header, rows);
const md = mdtable(mdJson, {
borders: true,
padding: 1,
});
result.push(md);
}
});

return result.join('\n\n');
}

/**
* Generate markdown tables by html string
* @param {string} html string
* @returns {string} string
*/
export function convert(html: string) {
try {
const json = Tabletojson.convert(html);
return transformJson(json);
} catch (e) {
console.error(e);
return '';
}
}

/**
* Generate markdown tables by a given URL
* @param {string} html string
* @returns {string} string
*/
/* istanbul ignore next */
export async function convertUrl(url: string) {
try {
const json = await Tabletojson.convertUrl(url);
return transformJson(json);
} catch (e) {
console.error(e);
return '';
}
}
40 changes: 40 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { describe, it, expect } from 'vitest';
import { convert } from '../src';

import fs from 'fs';
import path from 'path';

const tableHtml = fs.readFileSync(
path.resolve(process.cwd(), './test/table.html'),
{
encoding: 'utf-8',
}
);

const twoTableHtml = fs.readFileSync(
path.resolve(process.cwd(), './test/twoTable.html'),
{
encoding: 'utf-8',
}
);

describe('tabletomd', () => {
it('convert', () => {
expect(convert(tableHtml))
.equal(`| Company | Contact | Country |
|:---------------------------|:----------------|:--------|
| Alfreds Futterkiste | Maria Anders | Germany |
| Centro comercial Moctezuma | Francisco Chang | Mexico |`);
});
it('convert two table', () => {
expect(convert(twoTableHtml))
.equal(`| Company | Contact | Country |
|:---------------------------|:----------------|:--------|
| Alfreds Futterkiste | Maria Anders | Germany |
| Centro comercial Moctezuma | Francisco Chang | Mexico |
| Emil | Tobias | Linus |
|:-----|:-------|:------|
| 16 | 14 | 10 |`);
});
});
Loading

0 comments on commit 4913aeb

Please sign in to comment.