@stacksjs/ts-md 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,151 @@
1
+
2
+ ## v0.3.1...main
3
+
4
+ [compare changes](https://github.com/stacksjs/ts-starter/compare/v0.3.1...main)
5
+
6
+ ### 🚀 Enhancements
7
+
8
+ - Add `bun-plugin-dts-auto` ([c0c487b](https://github.com/stacksjs/ts-starter/commit/c0c487b))
9
+
10
+ ### ❤️ Contributors
11
+
12
+ - Chris <chrisbreuer93@gmail.com>
13
+
14
+ ## v0.3.0...main
15
+
16
+ [compare changes](https://github.com/stacksjs/ts-starter/compare/v0.3.0...main)
17
+
18
+ ### 🏡 Chore
19
+
20
+ - Fix isolatedDeclarations setting ([b87b6b1](https://github.com/stacksjs/ts-starter/commit/b87b6b1))
21
+ - Adjust urls ([0a40b72](https://github.com/stacksjs/ts-starter/commit/0a40b72))
22
+
23
+ ### ❤️ Contributors
24
+
25
+ - Chris <chrisbreuer93@gmail.com>
26
+
27
+ ## v0.2.1...main
28
+
29
+ [compare changes](https://github.com/stacksjs/ts-starter/compare/v0.2.1...main)
30
+
31
+ ### 🚀 Enhancements
32
+
33
+ - Add `noFallthroughCasesInSwitch` ([b9cfa30](https://github.com/stacksjs/ts-starter/commit/b9cfa30))
34
+ - Add `verbatimModuleSyntax` ([c495d17](https://github.com/stacksjs/ts-starter/commit/c495d17))
35
+ - Several updates ([f703179](https://github.com/stacksjs/ts-starter/commit/f703179))
36
+
37
+ ### 🩹 Fixes
38
+
39
+ - Properly use bun types ([7144221](https://github.com/stacksjs/ts-starter/commit/7144221))
40
+
41
+ ### 🏡 Chore
42
+
43
+ - Adjust badge links ([432aff7](https://github.com/stacksjs/ts-starter/commit/432aff7))
44
+ - Add `runs-on` options ([9a5b97f](https://github.com/stacksjs/ts-starter/commit/9a5b97f))
45
+ - Cache node_modules ([ba2f6ce](https://github.com/stacksjs/ts-starter/commit/ba2f6ce))
46
+ - Use `ubuntu-latest` for now ([1add684](https://github.com/stacksjs/ts-starter/commit/1add684))
47
+ - Minor updates ([1007cff](https://github.com/stacksjs/ts-starter/commit/1007cff))
48
+ - Lint ([d531bdc](https://github.com/stacksjs/ts-starter/commit/d531bdc))
49
+ - Remove bunx usage ([e1a5575](https://github.com/stacksjs/ts-starter/commit/e1a5575))
50
+ - Pass bun flag ([960976f](https://github.com/stacksjs/ts-starter/commit/960976f))
51
+ - Use defaults ([157455b](https://github.com/stacksjs/ts-starter/commit/157455b))
52
+ - Run typecheck using bun flag ([f22f3b1](https://github.com/stacksjs/ts-starter/commit/f22f3b1))
53
+ - Test ([0b3c3a1](https://github.com/stacksjs/ts-starter/commit/0b3c3a1))
54
+ - Use modern js for commitlint ([4bd6978](https://github.com/stacksjs/ts-starter/commit/4bd6978))
55
+ - Update worklows readme ([f54aae9](https://github.com/stacksjs/ts-starter/commit/f54aae9))
56
+ - Adjust readme ([92d7ff1](https://github.com/stacksjs/ts-starter/commit/92d7ff1))
57
+ - More updates ([0225587](https://github.com/stacksjs/ts-starter/commit/0225587))
58
+ - Add .zed settings for biome ([1688024](https://github.com/stacksjs/ts-starter/commit/1688024))
59
+ - Extend via alias ([b108d30](https://github.com/stacksjs/ts-starter/commit/b108d30))
60
+ - Lint ([d961b2a](https://github.com/stacksjs/ts-starter/commit/d961b2a))
61
+ - Minor updates ([e66d44a](https://github.com/stacksjs/ts-starter/commit/e66d44a))
62
+
63
+ ### ❤️ Contributors
64
+
65
+ - Chris <chrisbreuer93@gmail.com>
66
+
67
+ ## v0.2.0...main
68
+
69
+ [compare changes](https://github.com/stacksjs/ts-starter/compare/v0.2.0...main)
70
+
71
+ ### 🏡 Chore
72
+
73
+ - Remove unused action ([066f85a](https://github.com/stacksjs/ts-starter/commit/066f85a))
74
+ - Housekeeping ([fc4e24d](https://github.com/stacksjs/ts-starter/commit/fc4e24d))
75
+
76
+ ### ❤️ Contributors
77
+
78
+ - Chris <chrisbreuer93@gmail.com>
79
+
80
+ ## v0.1.1...main
81
+
82
+ [compare changes](https://github.com/stacksjs/ts-starter/compare/v0.1.1...main)
83
+
84
+ ### 🏡 Chore
85
+
86
+ - Adjust eslint config name ([53c2aa6](https://github.com/stacksjs/ts-starter/commit/53c2aa6))
87
+ - Set type module ([22dde14](https://github.com/stacksjs/ts-starter/commit/22dde14))
88
+
89
+ ### ❤️ Contributors
90
+
91
+ - Chris <chrisbreuer93@gmail.com>
92
+
93
+ ## v0.1.0...main
94
+
95
+ [compare changes](https://github.com/stacksjs/ts-starter/compare/v0.1.0...main)
96
+
97
+ ### 🏡 Chore
98
+
99
+ - Use correct cover image ([75bd3ae](https://github.com/stacksjs/ts-starter/commit/75bd3ae))
100
+
101
+ ### ❤️ Contributors
102
+
103
+ - Chris <chrisbreuer93@gmail.com>
104
+
105
+ ## v0.0.5...main
106
+
107
+ [compare changes](https://github.com/stacksjs/ts-starter/compare/v0.0.5...main)
108
+
109
+ ### 🚀 Enhancements
110
+
111
+ - Add pkgx deps ([319c066](https://github.com/stacksjs/ts-starter/commit/319c066))
112
+ - Use flat eslint config ([cdb0093](https://github.com/stacksjs/ts-starter/commit/cdb0093))
113
+
114
+ ### 🏡 Chore
115
+
116
+ - Fix badge ([bc3b000](https://github.com/stacksjs/ts-starter/commit/bc3b000))
117
+ - Minor updates ([78dc522](https://github.com/stacksjs/ts-starter/commit/78dc522))
118
+ - Housekeeping ([e1cba3b](https://github.com/stacksjs/ts-starter/commit/e1cba3b))
119
+ - Additional housekeeping ([f5dc625](https://github.com/stacksjs/ts-starter/commit/f5dc625))
120
+ - Add `.gitattributes` ([7080f8c](https://github.com/stacksjs/ts-starter/commit/7080f8c))
121
+ - Adjust deps ([cc71b42](https://github.com/stacksjs/ts-starter/commit/cc71b42))
122
+ - Adjust wording ([3bc54b3](https://github.com/stacksjs/ts-starter/commit/3bc54b3))
123
+ - Adjust readme cover ([e6acbb2](https://github.com/stacksjs/ts-starter/commit/e6acbb2))
124
+
125
+ ### ❤️ Contributors
126
+
127
+ - Chris <chrisbreuer93@gmail.com>
128
+
129
+ ## v0.0.5...main
130
+
131
+ [compare changes](https://github.com/stacksjs/ts-starter/compare/v0.0.5...main)
132
+
133
+ ### 🚀 Enhancements
134
+
135
+ - Add pkgx deps ([319c066](https://github.com/stacksjs/ts-starter/commit/319c066))
136
+ - Use flat eslint config ([cdb0093](https://github.com/stacksjs/ts-starter/commit/cdb0093))
137
+
138
+ ### 🏡 Chore
139
+
140
+ - Fix badge ([bc3b000](https://github.com/stacksjs/ts-starter/commit/bc3b000))
141
+ - Minor updates ([78dc522](https://github.com/stacksjs/ts-starter/commit/78dc522))
142
+ - Housekeeping ([e1cba3b](https://github.com/stacksjs/ts-starter/commit/e1cba3b))
143
+ - Additional housekeeping ([f5dc625](https://github.com/stacksjs/ts-starter/commit/f5dc625))
144
+ - Add `.gitattributes` ([7080f8c](https://github.com/stacksjs/ts-starter/commit/7080f8c))
145
+ - Adjust deps ([cc71b42](https://github.com/stacksjs/ts-starter/commit/cc71b42))
146
+ - Adjust wording ([3bc54b3](https://github.com/stacksjs/ts-starter/commit/3bc54b3))
147
+ - Adjust readme cover ([e6acbb2](https://github.com/stacksjs/ts-starter/commit/e6acbb2))
148
+
149
+ ### ❤️ Contributors
150
+
151
+ - Chris <chrisbreuer93@gmail.com>
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ # MIT License
2
+
3
+ Copyright (c) 2024 Open Web Foundation
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,121 @@
1
+ # ts-md
2
+
3
+ High-performance markdown parser and sanitizer built for Bun.
4
+
5
+ ## Features
6
+
7
+ - GitHub Flavored Markdown (GFM) support
8
+ - Tables, task lists, strikethrough
9
+ - Header ID generation
10
+ - Syntax highlighting support
11
+ - HTML sanitization
12
+ - Frontmatter parsing (YAML, TOML, JSON)
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ bun add ts-md
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ ### Basic Markdown Parsing
23
+
24
+ ```typescript
25
+ import { parseMarkdown } from 'ts-md'
26
+
27
+ const html = parseMarkdown('# Hello **world**')
28
+ // <h1 id="hello-world">Hello <strong>world</strong></h1>
29
+ ```
30
+
31
+ ### With Options
32
+
33
+ ```typescript
34
+ const html = parseMarkdown(markdown, {
35
+ gfm: true, // GitHub Flavored Markdown (default: true)
36
+ breaks: false, // Convert \n to <br> (default: false)
37
+ headerIds: true, // Generate header IDs (default: true)
38
+ headerPrefix: '', // Prefix for header IDs (default: '')
39
+ sanitize: false, // Sanitize HTML output (default: false)
40
+ highlight: (code, lang) => {
41
+ // Custom syntax highlighting
42
+ return highlightedCode
43
+ }
44
+ })
45
+ ```
46
+
47
+ ### HTML Sanitization
48
+
49
+ ```typescript
50
+ import { sanitizeHtml } from 'ts-md'
51
+
52
+ const clean = sanitizeHtml(userInput, {
53
+ allowedTags: ['p', 'strong', 'em', 'a', 'code'],
54
+ allowedAttributes: {
55
+ a: ['href', 'title']
56
+ },
57
+ allowedSchemes: ['http', 'https', 'mailto']
58
+ })
59
+ ```
60
+
61
+ ### Frontmatter Parsing
62
+
63
+ ```typescript
64
+ import { parseFrontmatter } from 'ts-md'
65
+
66
+ const content = `---
67
+ title: My Post
68
+ date: 2024-01-01
69
+ ---
70
+
71
+ # Content here`
72
+
73
+ const { data, content: markdown } = parseFrontmatter(content)
74
+ console.log(data.title) // 'My Post'
75
+ ```
76
+
77
+ ## Performance
78
+
79
+ Benchmark results against popular markdown parsers:
80
+
81
+ | Document Size | ts-md | markdown-it | marked | showdown |
82
+ |--------------|-------------------|-------------|---------|----------|
83
+ | Small (< 1KB) | 324B ops/sec | 112B ops/sec | 26B ops/sec | 14B ops/sec |
84
+ | Medium (~3KB) | 34.7B ops/sec | 17.7B ops/sec | 2.8B ops/sec | 2.8B ops/sec |
85
+ | Large (~50KB) | 1.81B ops/sec | 1.25B ops/sec | 16M ops/sec | 135M ops/sec |
86
+
87
+ **Performance vs markdown-it:**
88
+
89
+ - Small documents: 2.89x faster
90
+ - Medium documents: 1.96x faster
91
+ - Large documents: 1.45x faster
92
+
93
+ The parser uses a flat token stream architecture with position-based parsing for optimal performance.
94
+
95
+ ## Architecture
96
+
97
+ The markdown parser is built with several key optimizations:
98
+
99
+ - **Flat token stream**: Avoids nested object allocations for better cache locality
100
+ - **Position-based parsing**: Minimizes string allocations with substring operations
101
+ - **Optimized escapeHtml**: Fast-path for strings without special characters
102
+ - **Direct inline matching**: Efficient emphasis and link parsing
103
+ - **Recursive nested parsing**: Proper support for nested inline elements
104
+
105
+ ## API
106
+
107
+ ### `parseMarkdown(markdown: string, options?: MarkdownOptions): string`
108
+
109
+ Parse markdown to HTML.
110
+
111
+ ### `sanitizeHtml(html: string, options?: SanitizeOptions): string`
112
+
113
+ Sanitize HTML to prevent XSS attacks.
114
+
115
+ ### `parseFrontmatter(content: string): { data: any, content: string }`
116
+
117
+ Extract and parse frontmatter from markdown content. Supports YAML, TOML, and JSON formats.
118
+
119
+ ## License
120
+
121
+ MIT
package/bin/cli.ts ADDED
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env bun
2
+ import { readFileSync } from 'node:fs'
3
+ import { resolve } from 'node:path'
4
+ import { parseMarkdown, parseMarkdownWithFrontmatter, parseFrontmatter, parseYaml } from '../src/index'
5
+
6
+ const args = process.argv.slice(2)
7
+
8
+ if (args.length === 0 || args.includes('--help') || args.includes('-h')) {
9
+ console.log(`
10
+ ts-md - Fast, native Bun-powered markdown parser
11
+
12
+ USAGE:
13
+ md <file> [options]
14
+
15
+ OPTIONS:
16
+ --frontmatter, -f Parse frontmatter only
17
+ --yaml, -y Parse as YAML
18
+ --full Parse markdown with frontmatter
19
+ --gfm Enable GitHub Flavored Markdown
20
+ --breaks Enable line breaks
21
+ --json Output as JSON
22
+ --help, -h Show this help
23
+ --version, -v Show version
24
+
25
+ EXAMPLES:
26
+ md README.md # Parse markdown to HTML
27
+ md document.md --frontmatter # Extract frontmatter
28
+ md document.md --full # Parse with frontmatter
29
+ md data.yaml --yaml # Parse YAML
30
+ md README.md --gfm --breaks # Parse with GFM and breaks
31
+ `)
32
+ process.exit(0)
33
+ }
34
+
35
+ if (args.includes('--version') || args.includes('-v')) {
36
+ const pkg = JSON.parse(readFileSync(resolve(import.meta.dir, '../package.json'), 'utf-8'))
37
+ console.log(pkg.version)
38
+ process.exit(0)
39
+ }
40
+
41
+ const filePath = args.find(arg => !arg.startsWith('--') && !arg.startsWith('-'))
42
+ if (!filePath) {
43
+ console.error('Error: No file specified')
44
+ process.exit(1)
45
+ }
46
+
47
+ const fullPath = resolve(process.cwd(), filePath)
48
+ const content = readFileSync(fullPath, 'utf-8')
49
+
50
+ const isFrontmatter = args.includes('--frontmatter') || args.includes('-f')
51
+ const isYaml = args.includes('--yaml') || args.includes('-y')
52
+ const isFull = args.includes('--full')
53
+ const asJson = args.includes('--json')
54
+ const gfm = args.includes('--gfm')
55
+ const breaks = args.includes('--breaks')
56
+
57
+ try {
58
+ let result: any
59
+
60
+ if (isYaml) {
61
+ result = parseYaml(content)
62
+ }
63
+ else if (isFrontmatter) {
64
+ result = parseFrontmatter(content)
65
+ }
66
+ else if (isFull) {
67
+ result = parseMarkdownWithFrontmatter(content, { gfm, breaks })
68
+ }
69
+ else {
70
+ result = parseMarkdown(content, { gfm, breaks })
71
+ }
72
+
73
+ if (asJson) {
74
+ console.log(JSON.stringify(result, null, 2))
75
+ }
76
+ else if (typeof result === 'string') {
77
+ console.log(result)
78
+ }
79
+ else {
80
+ console.log(result)
81
+ }
82
+ }
83
+ catch (error) {
84
+ console.error('Error parsing file:', error)
85
+ process.exit(1)
86
+ }
@@ -0,0 +1,5 @@
1
+ import type { BinaryConfig } from './types';
2
+ export declare function getConfig(): Promise<BinaryConfig>;
3
+ export declare const defaultConfig: BinaryConfig;
4
+ // For backwards compatibility - synchronous access with default fallback
5
+ export declare const config: BinaryConfig;
@@ -0,0 +1,17 @@
1
+ import type { JsonObject, ParsedMarkdown } from './types';
2
+ /**
3
+ * Parse markdown with frontmatter
4
+ */
5
+ export declare function parse<T = JsonObject>(content: string): ParsedMarkdown<T>;
6
+ /**
7
+ * Stringify data to frontmatter markdown
8
+ */
9
+ export declare function stringify<T extends JsonObject = JsonObject>(data: T, content: string, format?: 'yaml' | 'toml'): string;
10
+ /**
11
+ * Check if content has frontmatter
12
+ */
13
+ export declare function hasFrontmatter(content: string): boolean;
14
+ /**
15
+ * Extract only the frontmatter (without parsing)
16
+ */
17
+ export declare function extractFrontmatter(content: string): string | null;
@@ -0,0 +1,14 @@
1
+ import { parse as parseFrontmatter } from './frontmatter';
2
+ import { parse as parseMarkdown } from './parser';
3
+ /**
4
+ * Parse markdown file with frontmatter in one go
5
+ */
6
+ export declare function parseMarkdownWithFrontmatter<T = Record<string, any>>(content: string, options?: { gfm?: boolean, breaks?: boolean }): { data: T, content: string, html: string };
7
+ export * as frontmatter from './frontmatter';
8
+ export { parse as parseFrontmatter, stringify as stringifyFrontmatter } from './frontmatter';
9
+ export * from './parser';
10
+ export { parse as parseMarkdown, parseSync as parseMarkdownSync } from './parser';
11
+ export * from './types';
12
+ export * as yaml from './yaml';
13
+ export { parse as parseYaml, stringify as stringifyYaml } from './yaml';
14
+ export default markdownParser;