Skip to content

Commit

Permalink
feat: parser (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Feb 20, 2024
1 parent a0aedbc commit a89a336
Show file tree
Hide file tree
Showing 38 changed files with 13,550 additions and 354 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
coverage
dist
lib/*.mjs
4 changes: 3 additions & 1 deletion .github/workflows/autofix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
- run: pnpm install
- name: Fix lint issues
run: pnpm lint:fix
- name: Build lib
run: pnpm build
- uses: autofix-ci/action@ea32e3a12414e6d3183163c3424a7d7a8631ad84
with:
commit-message: "chore: apply automated fixes"
commit-message: "chore: apply automated updates"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ dist
.eslintcache
*.log*
*.env*
deno.lock
.tmp
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test/snapshots/**/*.*
test/fixtures/**/*.*
lib/*.mjs
58 changes: 57 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c)
Copyright (c) Pooya Parsa <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -19,3 +19,59 @@ 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.

---

markdown-it - https://github.com/markdown-it/markdown-it/blob/master/LICENSE

Copyright (c) 2014 Vitaly Puzrin, Alex Kocharin.

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.

---

mdast-util-from-markdown - https://github.com/syntax-tree/mdast-util-from-markdown/blob/main/license
micromark-extension-gfm - https://github.com/micromark/micromark-extension-gfm/blob/main/license
mdast-util-gfm - https://github.com/syntax-tree/mdast-util-gfm/blob/main/license

(The MIT License)

Copyright (c) 2020 Titus Wormer <[email protected]>

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.
64 changes: 54 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ Just simple markdown utils!
## 💡 Why?

> Markdown is intended to be as easy-to-read and easy-to-write as is. Readability is emphasized above all else. A Markdown-formatted document should be publishable as-is, as plain text [^1]. Any sequence of characters is a valid Markdown document [^2].
> [^1]: https://daringfireball.net/projects/markdown/
> [^2]: https://spec.commonmark.org/0.31.2/#characters-and-lines
While Markdown is designed to be simple, I often find myself in situations where there is simply no tool to allow programmatically working with Markdown syntax without dealing with complex and strict AST objects and choosing between dozens of available tools and extensions. Often, not even worth pursuing ideas around Markdown.

Expand Down Expand Up @@ -49,13 +47,7 @@ import { md } from "omark";
const { md } = require("omark");
```

<!-- AUTOMD_START generator="jsdocs" group="parsing" -->



<!-- AUTOMD_END -->

<!-- AUTOMD_START generator="jsdocs" group="render_utils" -->
<!-- automd:jsdocs src="./src/index" group="render_utils" -->

## Render Utils

Expand Down Expand Up @@ -207,7 +199,56 @@ md.table({
```


<!-- AUTOMD_END -->
<!-- /automd -->

<!-- automd:jsdocs src="./src/parser/index" group="parsing_utils" -->

## Parsing Utils

### `initMarkdownItParser(options)`

Create parser with [markdown-it](https://github.com/markdown-it/markdown-it).

**WARNING**: The returned tree structure is unstable.

**Example:**

```ts
import { initMarkdownItParser } from "omark/parser";
const parser = await initMarkdownItParser();
const { tree } = parser.parse("# Hello, *world*!");
```

### `initMd4wParser(opts)`

Create parser with [md4w](https://github.com/ije/md4w).

**WARNING**: The returned tree structure is unstable.

**Example:**

```ts
import { initMd4wParser } from "omark/parser";
const parser = await initMd4wParser();
const { tree } = parser.parse("# Hello, *world*!");
```

### `initMdAstParser(opts)`

Create parser with [mdast-util-from-markdown](https://github.com/syntax-tree/mdast-util-from-markdown).

**WARNING**: The returned tree structure is unstable.

**Example:**

```ts
import { initMdAstParser } from "omark/parser";
const parser = await initMdAstParser();
const { tree } = parser.parse("# Hello, *world*!");
```


<!-- /automd -->

## Development

Expand Down Expand Up @@ -235,3 +276,6 @@ Published under [MIT License](./LICENSE).
[bundle-src]: https://img.shields.io/bundlephobia/minzip/omark?style=flat&colorA=18181B&colorB=F0DB4F
[bundle-href]: https://bundlephobia.com/result?p=omark -->

[^1]: https://daringfireball.net/projects/markdown/
[^2]: https://spec.commonmark.org/0.31.2/#characters-and-lines
23 changes: 23 additions & 0 deletions build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { defineBuildConfig } from "unbuild";

export default defineBuildConfig({
declaration: true,
rollup: { emitCJS: true },
entries: ["src/index", "src/parser"],
externals: ["omark", "mdast-util-from-markdown", "markdown-it"],
hooks: {
async "build:before"() {
const { build } = await import("esbuild");
await build({
entryPoints: ["lib/markdown-it.ts", "lib/mdast.ts"],
outExtension: { ".js": ".mjs" },
bundle: true,
outdir: "lib",
format: "esm",
platform: "neutral",
mainFields: ["module", "main"],
minify: true,
});
},
},
});
10 changes: 10 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "https://deno.land/x/deno/cli/schemas/config-file.v1.json",
"imports": {
"markdown-it": "https://esm.sh/[email protected]",
"mdast": "https://esm.sh/@types/mdast",
"mdast-util-from-markdown": "https://esm.sh/[email protected]",
"omark/lib/markdown-it": "./lib/markdown-it.mjs",
"omark/lib/mdast": "./lib/mdast.mjs"
}
}
16 changes: 16 additions & 0 deletions lib/markdown-it.mjs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/markdown-it.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "markdown-it";
4 changes: 4 additions & 0 deletions lib/mdast.mjs

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions lib/mdast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { fromMarkdown } from "mdast-util-from-markdown";
export { gfm } from "micromark-extension-gfm";
export { gfmFromMarkdown } from "mdast-util-gfm";
33 changes: 29 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,33 @@
"sideEffects": false,
"type": "module",
"exports": {
"./lib/*": "./lib/*.mjs",
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
},
"./parser": {
"types": "./dist/parser.d.ts",
"import": "./dist/parser.mjs",
"require": "./dist/parser.cjs"
}
},
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"dist"
"dist",
"lib",
"!lib/*.ts",
"parser.d.ts"
],
"scripts": {
"bench:bun": "bun --bun ./test/parse.bench.ts",
"bench:deno": "deno bench -A --unstable-sloppy-imports test/parse.bench.ts",
"bench:node": "vitest bench",
"build": "automd && unbuild",
"dev": "vitest dev --coverage",
"dev": "vitest dev -u",
"lint": "eslint --cache --ext .ts,.js,.mjs,.cjs . && prettier -c src test",
"lint:fix": "eslint --cache --ext .ts,.js,.mjs,.cjs . --fix && prettier -c src test -w",
"prepack": "pnpm build",
Expand All @@ -30,18 +42,31 @@
"test": "pnpm lint && pnpm test:types && vitest run --coverage",
"test:types": "tsc --noEmit --skipLibCheck"
},
"dependencies": {
"md4w": "^0.1.9"
},
"devDependencies": {
"@types/markdown-it": "^13.0.7",
"@types/mdast": "^4.0.3",
"@types/node": "^20.11.19",
"@vitest/coverage-v8": "^1.3.0",
"automd": "^0.3.0",
"changelogen": "^0.5.5",
"esbuild": "^0.20.1",
"eslint": "^8.56.0",
"eslint-config-unjs": "^0.2.1",
"jiti": "^1.21.0",
"markdown-it": "^14.0.0",
"mdast-util-from-markdown": "^2.0.0",
"mdast-util-gfm": "^3.0.0",
"micromark-extension-gfm": "^3.0.0",
"mitata": "^0.1.10",
"ohash": "^1.1.3",
"prettier": "^3.2.5",
"punycode.js": "^2.3.1",
"typescript": "^5.3.3",
"unbuild": "^2.0.0",
"vite-tsconfig-paths": "^4.3.1",
"vitest": "^1.3.0"
},
"packageManager": "[email protected]"
}
}
1 change: 1 addition & 0 deletions parser.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./dist/parser";
Loading

0 comments on commit a89a336

Please sign in to comment.