Skip to content

Commit

Permalink
Move to a single file
Browse files Browse the repository at this point in the history
  • Loading branch information
thetarnav committed Mar 4, 2024
1 parent 592435f commit 578b1d4
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 267 deletions.
21 changes: 8 additions & 13 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import path from "node:path"
import url from "node:url"
import ts from "typescript"

const dirname = path.dirname(url.fileURLToPath(import.meta.url))
const src_dir = path.join(dirname, "mds")
const src_entry = path.join(src_dir, "mds.js")
const dirname = path.dirname(url.fileURLToPath(import.meta.url))
const file_js_path = path.join(dirname, "smd.js")
const file_dts_path = path.join(dirname, "smd.d.ts")
const file_map_path = path.join(dirname, "smd.d.ts.map")

/** @type {ts.CompilerOptions} */
const ts_options = {
Expand All @@ -23,18 +24,12 @@ const ts_options = {
function main() {
const begin = performance.now()

// Remove old .d.ts files except t.d.ts
const files = fs.readdirSync(src_dir)
for (const file of files) {
if ((file.endsWith(".d.ts") && file !== "t.d.ts") ||
file.endsWith(".d.ts.map")
) {
fs.unlinkSync(path.join(src_dir, file))
}
}
// Remove old .d.ts files
if (fs.existsSync(file_dts_path)) fs.unlinkSync(file_dts_path)
if (fs.existsSync(file_map_path)) fs.unlinkSync(file_map_path)

// Emit d.ts files
const program = ts.createProgram([src_entry], ts_options)
const program = ts.createProgram([file_js_path], ts_options)
program.emit()
console.log(`DTS complete in ${Math.ceil(performance.now() - begin)}ms`)
}
Expand Down
54 changes: 0 additions & 54 deletions mds/t.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion mds/t.js

This file was deleted.

12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
"sideEffects": false,
"type": "module",
"files": [
"mds"
"./smd.js",
"./smd.d.ts",
"./smd.d.ts.map"
],
"module": "./mds/mds.js",
"types": "./mds/mds.d.ts",
"module": "./smd.js",
"types": "./smd.d.ts",
"exports": {
"import": {
"types": "./mds/mds.d.ts",
"default": "./mds/mds.js"
"types": "./smd.d.ts",
"default": "./smd.js"
}
},
"scripts": {
Expand Down
12 changes: 6 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Install `streaming-markdown` package from npm.
npm install streaming-markdown
```

*Or just copy [**`mds`**](https://github.com/thetarnav/streaming-markdown/blob/main/mds) dir to your project.*
*Or just copy [**`smd`**](https://github.com/thetarnav/streaming-markdown/blob/main/smd.js) file to your project.*

## Usage

Expand All @@ -23,19 +23,19 @@ It's single argument is a `Renderer` object, which is an interface to render the
There are two built-in renderers—`default_renderer` and `logger_renderer`—that you can try at first.

```js
import * as mds from "streaming-markdown"
import * as smd from "streaming-markdown"

const element = document.getElementById("markdown")
const renderer = mds.default_renderer(element)
const parser = mds.parser(renderer)
const renderer = smd.default_renderer(element)
const parser = smd.parser(renderer)
```

### `write` function

Then, you can start streaming markdown to the `Parser` by calling `parser_write` function with the chunk of markdown string.

```js
mds.parser_write(parser, "# Streaming Markdown\n\n")
smd.parser_write(parser, "# Streaming Markdown\n\n")
```

*You can write **as many times as you want** to stream the markdown.*
Expand All @@ -56,7 +56,7 @@ Finally, you can end the stream by calling `end` function.
It will reset the `Parser` state and flush the remaining markdown.

```js
mds.parser_end(parser)
smd.parser_end(parser)
```

## TODO
Expand Down
109 changes: 82 additions & 27 deletions mds/mds.js → smd.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ Copyright 2024 Damian Tarnawski
https://github.com/thetarnav/streaming-markdown
*/

export * from "./t.js"

export const
DOCUMENT = 1, // 1
PARAGRAPH = 2, // 2
Expand Down Expand Up @@ -124,10 +122,60 @@ export function attr_to_html_attr(type) {
}

/**
* @typedef {import("./t.js").Any_Renderer} Any_Renderer
* @typedef {import("./t.js").Parser } Parser
* @typedef {object } Parser
* @property {Any_Renderer} renderer - {@link Renderer} interface
* @property {string } text - Text to be added to the last token in the next flush
* @property {string } pending - Characters for identifying tokens
* @property {Token[] } types - Current token and it's parents (a slice of a tree)
* @property {number } len - Number of tokens in types without root
* @property {0 | 1 } code_fence_body - For {@link Token.Code_Fence} parsing
* @property {number } backticks_count
* @property {number } blockquote_idx - For Blockquote parsing
* @property {string } hr_char - For horizontal rule parsing
* @property {number } hr_chars - For horizontal rule parsing
*/

/**
* @template T
* @callback Renderer_Add_Token
* @param {T } data
* @param {Token} type
* @returns {void } */

/**
* @template T
* @callback Renderer_End_Token
* @param {T } data
* @returns {void } */

/**
* @template T
* @callback Renderer_Add_Text
* @param {T } data
* @param {string} text
* @returns {void } */

/**
* @template T
* @callback Renderer_Set_Attr
* @param {T } data
* @param {Attr } type
* @param {string} value
* @returns {void } */

/**
* The renderer interface.
* @template T
* @typedef {object } Renderer
* @property {T } data
* @property {Renderer_Add_Token<T>} add_token
* @property {Renderer_End_Token<T>} end_token
* @property {Renderer_Add_Text <T>} add_text
* @property {Renderer_Set_Attr <T>} set_attr
*/

/** @typedef {Renderer<any>} Any_Renderer */

/**
* Makes a new Parser object.
* @param {Any_Renderer} renderer
Expand All @@ -140,7 +188,7 @@ export function parser(renderer) {
types : /**@type {*}*/([DOCUMENT,,,,,]),
len : 0,
code_fence_body: 0,
newline_blockquote_idx: 0,
blockquote_idx: 0,
hr_char : '',
hr_chars : 0,
backticks_count: 0,
Expand Down Expand Up @@ -195,7 +243,7 @@ function _parser_into_line_break(p) {
parser_add_text(p)
p.len += 1
p.types[p.len] = LINE_BREAK
p.newline_blockquote_idx = 0
p.blockquote_idx = 0
}

/**
Expand Down Expand Up @@ -223,30 +271,30 @@ export function parser_write(p, chunk) {
case ">":
p.pending = char

while (p.newline_blockquote_idx+1 < p.len-1) {
p.newline_blockquote_idx += 1
if (p.types[p.newline_blockquote_idx] === BLOCKQUOTE) {
while (p.blockquote_idx+1 < p.len-1) {
p.blockquote_idx += 1
if (p.types[p.blockquote_idx] === BLOCKQUOTE) {
continue chars
}
}

p.len -= 1 // end line break

while (p.newline_blockquote_idx < p.len) {
while (p.blockquote_idx < p.len) {
parser_end_token(p)
}

p.newline_blockquote_idx += 1
p.blockquote_idx += 1
p.backticks_count = 0
parser_add_token(p, BLOCKQUOTE)
continue
case "\n":
p.len -= 1 // end line break

while (p.newline_blockquote_idx < p.len) {
while (p.blockquote_idx < p.len) {
parser_end_token(p)
}
p.newline_blockquote_idx = 0
p.blockquote_idx = 0
p.backticks_count = 0
p.pending = char
continue
Expand Down Expand Up @@ -306,14 +354,14 @@ export function parser_write(p, chunk) {
case '>':
p.pending = char

while (p.newline_blockquote_idx+1 <= p.len) {
p.newline_blockquote_idx += 1
if (p.types[p.newline_blockquote_idx] === BLOCKQUOTE) {
while (p.blockquote_idx+1 <= p.len) {
p.blockquote_idx += 1
if (p.types[p.blockquote_idx] === BLOCKQUOTE) {
continue chars
}
}

p.newline_blockquote_idx += 1
p.blockquote_idx += 1
parser_add_token(p, BLOCKQUOTE)
continue
/* Horizontal Rule
Expand Down Expand Up @@ -769,12 +817,17 @@ export function parser_write(p, chunk) {
parser_add_text(p)
}


/**
* @typedef {import("./t.js").Default_Renderer } Default_Renderer
* @typedef {import("./t.js").Default_Renderer_Add_Token} Default_Renderer_Add_Token
* @typedef {import("./t.js").Default_Renderer_End_Token} Default_Renderer_End_Token
* @typedef {import("./t.js").Default_Renderer_Add_Text } Default_Renderer_Add_Text
* @typedef {import("./t.js").Default_Renderer_Set_Attr } Default_Renderer_Set_Attr
* @typedef {object} Default_Renderer_Data
* @property {HTMLElement[]} nodes
* @property {number } index
*
* @typedef {Renderer <Default_Renderer_Data>} Default_Renderer
* @typedef {Renderer_Add_Token<Default_Renderer_Data>} Default_Renderer_Add_Token
* @typedef {Renderer_End_Token<Default_Renderer_Data>} Default_Renderer_End_Token
* @typedef {Renderer_Add_Text <Default_Renderer_Data>} Default_Renderer_Add_Text
* @typedef {Renderer_Set_Attr <Default_Renderer_Data>} Default_Renderer_Set_Attr
*/

/**
Expand Down Expand Up @@ -848,11 +901,13 @@ export function default_set_attr(data, type, value) {


/**
* @typedef {import("./t.js").Logger_Renderer } Logger_Renderer
* @typedef {import("./t.js").Logger_Renderer_Add_Token} Logger_Renderer_Add_Token
* @typedef {import("./t.js").Logger_Renderer_End_Token} Logger_Renderer_End_Token
* @typedef {import("./t.js").Logger_Renderer_Add_Text } Logger_Renderer_Add_Text
* @typedef {import("./t.js").Logger_Renderer_Set_Attr } Logger_Renderer_Set_Attr
* @typedef {undefined} Logger_Renderer_Data
*
* @typedef {Renderer <Logger_Renderer_Data>} Logger_Renderer
* @typedef {Renderer_Add_Token<Logger_Renderer_Data>} Logger_Renderer_Add_Token
* @typedef {Renderer_End_Token<Logger_Renderer_Data>} Logger_Renderer_End_Token
* @typedef {Renderer_Add_Text <Logger_Renderer_Data>} Logger_Renderer_Add_Text
* @typedef {Renderer_Set_Attr <Logger_Renderer_Data>} Logger_Renderer_Set_Attr
*/

/** @returns {Logger_Renderer} */
Expand Down
Loading

0 comments on commit 578b1d4

Please sign in to comment.