11 Commits

Author SHA1 Message Date
Morten Olsen
11f76a7378 refact: ink based terminal view (#17) 2025-05-19 16:22:59 +02:00
morten-olsen
4514972880 docs: generated README 2025-05-19 08:32:21 +00:00
Morten Olsen
c7b9abf868 feat: add javascript code block support (#13) 2025-05-19 10:31:12 +02:00
morten-olsen
bf14ef97b8 docs: generated README 2025-05-19 07:15:52 +00:00
Morten Olsen
ab2bb38f39 docs: add road-map 2025-05-19 09:14:47 +02:00
Morten Olsen
1d055e49f1 feat: rename 2025-05-19 09:07:21 +02:00
Morten Olsen
b308c7f9fe fix: align yaml option with documentation 2025-05-18 22:36:25 +02:00
Morten Olsen
ba56b66222 fix: yaml parsing 2025-05-18 22:29:50 +02:00
Morten Olsen
e0707e74fb docs: stuff 2025-05-18 22:18:15 +02:00
Morten Olsen
6b74a28989 docs: improved docs 2025-05-18 21:26:05 +02:00
morten-olsen
ad342e5f10 docs: generated README 2025-05-18 19:12:53 +00:00
29 changed files with 1598 additions and 120 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
/node_modules/
/dist/
/*.html

363
README.md
View File

@@ -21,12 +21,51 @@ It allows developers to create API documentation that is always accurate and up-
* **Tutorials & Guides:** Build step-by-step guides where each HTTP interaction is shown with its real output.
* **Rapid Prototyping:** Quickly experiment with APIs and document your findings.
## Roadmap
* **Programmatic API** Use `http.md` inside existing scripts and pipelines
* **Environment Varaiables** Support using the runners environment variables in templates
* **JavaScript script support** Add JavaScript code blocks with execution, which will allow more advanced use-cases
* **Asserts** Add the ability to make HTTP assertions to use the document for testing
* **Templates** Write re-usable templates which can be used in documents
## Content
* [Key Features](#key-features)
* [Use Cases](#use-cases)
* [Roadmap](#roadmap)
* [Content](#content)
* [Installation](#installation)
* [Getting Started](#getting-started)
* [Your First Request](#your-first-request)
* [Rendering Documents](#rendering-documents)
* [Core Concepts](#core-concepts)
* [HTTP Request Blocks](#http-request-blocks)
* [The `::response` Directive](#the-response-directive)
* [Request IDs](#request-ids)
* [Templating with Handlebars](#templating-with-handlebars)
* [Available Variables for Templating](#available-variables-for-templating)
* [Templating Examples](#templating-examples)
* [Managing Documents](#managing-documents)
* [Embedding Other Documents (`::md`)](#embedding-other-documents-md)
* [Advanced Usage](#advanced-usage)
* [Using Input Variables](#using-input-variables)
* [JavaScript Execution](#javascript-execution)
* [HTTP Block Configuration Options](#http-block-configuration-options)
* [Directive Options](#directive-options)
* [`::response` Directive Options](#response-directive-options)
* [`::md[{file}]` Directive Options](#mdfile-directive-options)
* [`::input[{name}]` Directive Options](#inputname-directive-options)
* [Command-Line Interface (CLI)](#command-line-interface-cli)
* [`http.md dev <source_file.md>`](#httpmd-dev-source_filemd)
* [`http.md build <source_file.md> <output_file.md>`](#httpmd-build-source_filemd-output_filemd)
## Installation
Install `http.md` globally using npm:
```shell
npm i -g @morten-olsen/httpmd
npm i -g @morten-olsen/http.md
```
## Getting Started
@@ -63,13 +102,13 @@ You have two primary ways to render your `http.md` file:
For a development server that outputs to your terminal and watches for changes:
```shell
httpmd dev example.md
http.md dev example.md
```
With watch mode:
```shell
httpmd dev --watch example.md
http.md dev --watch example.md
```
This command will process `example.md`, execute the HTTP requests, and print the resulting markdown (with responses filled in) to the terminal. With `--watch`, any changes to `example.md` will trigger a re-run.
@@ -78,13 +117,13 @@ You have two primary ways to render your `http.md` file:
To generate a new markdown file with the responses and templated values rendered:
```shell
httpmd build example.md output.md
http.md build example.md output.md
```
With watch mode:
```shell
httpmd build --watch example.md output.md
http.md build --watch example.md output.md
```
This creates `output.md`, which is a static snapshot of `example.md` after all requests have been executed and templating applied. This file is suitable for version control, sharing, or integration with static site generators.
@@ -105,14 +144,14 @@ Content-Type: application/json
And here is the response:
```
```http
HTTP/200 OK
access-control-allow-credentials: true
access-control-allow-origin: *
connection: keep-alive
content-length: 556
content-length: 557
content-type: application/json
date: Sun, 18 May 2025 18:55:55 GMT
date: Mon, 19 May 2025 08:31:44 GMT
server: gunicorn/19.9.0
{
@@ -129,12 +168,12 @@ server: gunicorn/19.9.0
"Host": "httpbin.org",
"Sec-Fetch-Mode": "cors",
"User-Agent": "node",
"X-Amzn-Trace-Id": "Root=1-682a2d3b-244883ec40275d5e642566d6"
"X-Amzn-Trace-Id": "Root=1-682aec70-4f9d0a877f1453210a7009b6"
},
"json": {
"greeting": "Hello, http.md!"
},
"origin": "13.64.151.43",
"origin": "40.71.224.172",
"url": "https://httpbin.org/post"
}
@@ -193,23 +232,23 @@ You can assign a unique ID to an `http` request block. This allows you to:
1. Reference its specific response in a `::response` directive.
2. Access its request and response data in [Templating](https://www.google.com/search?q=%23templating-with-handlebars) via the `requests` and `responses` dictionaries.
To add an ID, include `id=yourUniqueId` in the `http` block's info string:
To add an ID, include `#yourUniqueId` or `id=yourUniqueId` in the `http` block's info string:
````markdown
# Document with Multiple Requests
First, create a resource:
```http id=createUser
```http #createUser,yaml,json
POST https://httpbin.org/post
Content-Type: application/json
{"username": "alpha"}
username: alpha
```
Then, fetch a different resource:
```http id=getItem
```http #getItem
GET https://httpbin.org/get?item=123
```
@@ -221,6 +260,96 @@ Response from getting the item:
````
<details>
<summary>Output</summary>
````markdown
# Document with Multiple Requests
First, create a resource:
```http
POST https://httpbin.org/post
Content-Type: application/json
{"username":"alpha"}
```
Then, fetch a different resource:
```http
GET https://httpbin.org/get?item=123
```
Response from creating the user:
```http
HTTP/200 OK
access-control-allow-credentials: true
access-control-allow-origin: *
connection: keep-alive
content-length: 502
content-type: application/json
date: Mon, 19 May 2025 08:31:44 GMT
server: gunicorn/19.9.0
{
"args": {},
"data": "username: alpha",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "br, gzip, deflate",
"Accept-Language": "*",
"Content-Length": "15",
"Content-Type": "application/json",
"Host": "httpbin.org",
"Sec-Fetch-Mode": "cors",
"User-Agent": "node",
"X-Amzn-Trace-Id": "Root=1-682aec70-52423dd76328a4e37066ba0e"
},
"json": null,
"origin": "40.71.224.172",
"url": "https://httpbin.org/post"
}
```
Response from getting the item:
```http
HTTP/200 OK
access-control-allow-credentials: true
access-control-allow-origin: *
connection: keep-alive
content-length: 383
content-type: application/json
date: Mon, 19 May 2025 08:31:44 GMT
server: gunicorn/19.9.0
{
"args": {
"item": "123"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "br, gzip, deflate",
"Accept-Language": "*",
"Host": "httpbin.org",
"Sec-Fetch-Mode": "cors",
"User-Agent": "node",
"X-Amzn-Trace-Id": "Root=1-682aec70-1509df0125913c7c042c1116"
},
"origin": "40.71.224.172",
"url": "https://httpbin.org/get?item=123"
}
```
````
</details>
## Templating with Handlebars
`http.md` uses [Handlebars](https://handlebarsjs.com/) for templating, allowing you to create dynamic content within your markdown files. You can inject data from request responses, input variables, and other requests into your HTTP blocks or general markdown text.
@@ -257,21 +386,21 @@ Within your markdown document, the following variables are available in the Hand
* **`input`** (Object): A dictionary of variables passed to `http.md` via the command line using the `-i` or `--input` flag.
* Example: If you run `httpmd dev -i userId=123 -i apiKey=secret myfile.md`, you can use `{{input.userId}}` and `{{input.apiKey}}`.
* Example: If you run `http.md dev -i userId=123 -i apiKey=secret myfile.md`, you can use `{{input.userId}}` and `{{input.apiKey}}`.
### Templating Examples
**1. Using a value from a previous response in a new request:**
````markdown
```http id=createItem json
```http #createItem,json
POST https://httpbin.org/post
Content-Type: application/json
{"name": "My New Item"}
```
The new item ID is: {{responses.createItem.body.json.name}}
The new item ID is: {{response.body.json.name}}
Now, let's fetch the item using a (mocked) ID from the response:
@@ -283,6 +412,61 @@ GET https://httpbin.org/anything/{{responses.createItem.body.json.name}}
````
<details>
<summary>Output</summary>
````markdown
```http
POST https://httpbin.org/post
Content-Type: application/json
{"name": "My New Item"}
```
The new item ID is: My New Item
Now, let's fetch the item using a (mocked) ID from the response:
```http
GET https://httpbin.org/anything/My New Item
```
```http
HTTP/200 OK
access-control-allow-credentials: true
access-control-allow-origin: *
connection: keep-alive
content-length: 453
content-type: application/json
date: Mon, 19 May 2025 08:31:44 GMT
server: gunicorn/19.9.0
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "br, gzip, deflate",
"Accept-Language": "*",
"Host": "httpbin.org",
"Sec-Fetch-Mode": "cors",
"User-Agent": "node",
"X-Amzn-Trace-Id": "Root=1-682aec70-0086cbec627144c72e8dd560"
},
"json": null,
"method": "GET",
"origin": "40.71.224.172",
"url": "https://httpbin.org/anything/My New Item"
}
```
````
</details>
*(Note: `httpbin.org/post` wraps the JSON sent in a "json" field in its response. If your API returns the ID directly at the root of the JSON body, you'd use `{{responses.createItem.body.id}}` assuming the `createItem` request had the `json` option.)*
**2. Displaying a status code in markdown text:**
@@ -310,9 +494,10 @@ The requests from the embedded document are processed, and their `request` and `
Assume `_shared_requests.md` contains:
````markdown
```http id=sharedGetRequest
```http #sharedGetRequest
GET https://httpbin.org/get
```
````
Then, in `main.md`:
@@ -324,7 +509,7 @@ Let's include some shared requests:
::md[./_shared_requests.md]
The shared GET request returned:
The shared GET request returned: {{response.statusText}}
Now, a request specific to this document:
@@ -332,12 +517,76 @@ Now, a request specific to this document:
POST https://httpbin.org/post
Content-Type: application/json
{"dataFromMain": "someValue", "sharedUrl": ""}
{"dataFromMain": "someValue", "sharedUrl": "{{requests.sharedGetRequest.url}}"}
```
::response
````
<details>
<summary>Output</summary>
````markdown
# Main Document
Let's include some shared requests:
```http
GET https://httpbin.org/get
```
The shared GET request returned: OK
Now, a request specific to this document:
```http
POST https://httpbin.org/post
Content-Type: application/json
{"dataFromMain": "someValue", "sharedUrl": "https://httpbin.org/get"}
```
```http
HTTP/200 OK
access-control-allow-credentials: true
access-control-allow-origin: *
connection: keep-alive
content-length: 642
content-type: application/json
date: Mon, 19 May 2025 08:31:44 GMT
server: gunicorn/19.9.0
{
"args": {},
"data": "{\"dataFromMain\": \"someValue\", \"sharedUrl\": \"https://httpbin.org/get\"}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "br, gzip, deflate",
"Accept-Language": "*",
"Content-Length": "69",
"Content-Type": "application/json",
"Host": "httpbin.org",
"Sec-Fetch-Mode": "cors",
"User-Agent": "node",
"X-Amzn-Trace-Id": "Root=1-682aec70-2a4b678f42e2be503f693fd5"
},
"json": {
"dataFromMain": "someValue",
"sharedUrl": "https://httpbin.org/get"
},
"origin": "40.71.224.172",
"url": "https://httpbin.org/post"
}
```
````
</details>
When `main.md` is processed, `_shared_requests.md` will be embedded, its `sharedGetRequest` will be executed, and its data will be available for templating.
## Advanced Usage
@@ -349,7 +598,7 @@ You can pass external data into your `http.md` documents using the `-i` (or `--i
**CLI Command:**
```shell
httpmd build mydoc.md output.md -i baseUrl=https://api.production.example.com -i apiKey=YOUR_SECRET_KEY
http.md build mydoc.md output.md -i baseUrl=https://api.production.example.com -i apiKey=YOUR_SECRET_KEY
```
**Markdown Usage (`mydoc.md`):**
@@ -365,6 +614,68 @@ Authorization: Bearer
**Security Note:** For sensitive data like API keys, using input variables is highly recommended over hardcoding them in your markdown files. Avoid committing files with plaintext secrets; instead, provide them at runtime via the CLI.
### JavaScript Execution
You can execute `javascript` blocks by adding a `run` option which allows programmatically changing the context, making request assertions and solve other more advanced use cases
**Example:**
````markdown
```javascript run
input.test = "Hello World";
```
::input[test]
```http json
POST https://httpbin.org/post
{"input": "{{input.test}}"}
```
```javascript run,hidden
// Use chai's `expect`, `assert` or `should` to make assumptions
expect(response.body.json.input).to.equal("Hello World");
```
````
<details>
<summary>Output</summary>
````markdown
```javascript
input.test = "Hello World";
```
```
test=Hello World
```
```http
POST https://httpbin.org/post
{"input": "Hello World"}
```
````
</details>
**Options:**
* `run`: If present the code block will be executed
* Example: ` ```javascript run `
* `hidden`: If present the code block will not be included in the resulting output
* Example: ` ```javascript hidden `
* `output`: If present the code blocks return value will be rendered as a `yaml` code block
### HTTP Block Configuration Options
You can configure the behavior of each `http` code block by adding options to its info string, separated by commas.
@@ -458,9 +769,9 @@ The `::input` directive is used to declare expected input variables
## Command-Line Interface (CLI)
The `httpmd` tool provides the following commands:
The `http.md` tool provides the following commands:
### `httpmd dev <source_file.md>`
### `http.md dev <source_file.md>`
Processes the `<source_file.md>`, executes all HTTP requests, resolves templates, and prints the resulting markdown to the **terminal (stdout)**.
@@ -472,10 +783,10 @@ Processes the `<source_file.md>`, executes all HTTP requests, resolves templates
**Example:**
```shell
httpmd dev api_tests.md --watch -i host=localhost:3000
http.md dev api_tests.md --watch -i host=localhost:3000
```
### `httpmd build <source_file.md> <output_file.md>`
### `http.md build <source_file.md> <output_file.md>`
Processes the `<source_file.md>`, executes all HTTP requests, resolves templates, and saves the resulting markdown to `<output_file.md>`.
@@ -487,5 +798,5 @@ Processes the `<source_file.md>`, executes all HTTP requests, resolves templates
**Example:**
```shell
httpmd build official_api_docs.md public/api_docs_v1.md -i version=v1.0
http.md build official_api_docs.md public/api_docs_v1.md -i version=v1.0
```

View File

@@ -21,12 +21,24 @@ It allows developers to create API documentation that is always accurate and up-
- **Tutorials & Guides:** Build step-by-step guides where each HTTP interaction is shown with its real output.
- **Rapid Prototyping:** Quickly experiment with APIs and document your findings.
## Roadmap
- **Programmatic API** Use `http.md` inside existing scripts and pipelines
- **Environment Varaiables** Support using the runners environment variables in templates
- **JavaScript script support** Add JavaScript code blocks with execution, which will allow more advanced use-cases
- **Asserts** Add the ability to make HTTP assertions to use the document for testing
- **Templates** Write re-usable templates which can be used in documents
## Content
::toc
## Installation
Install `http.md` globally using npm:
```shell
npm i -g @morten-olsen/httpmd
npm i -g @morten-olsen/http.md
```
## Getting Started
@@ -39,7 +51,7 @@ Create a file named `example.md`:
::raw-md[./examples/getting-started.md]
### Rendering Documents
### Rendering Document
You have two primary ways to render your `http.md` file:
@@ -47,13 +59,13 @@ You have two primary ways to render your `http.md` file:
For a development server that outputs to your terminal and watches for changes:
```shell
httpmd dev example.md
http.md dev example.md
```
With watch mode:
```shell
httpmd dev --watch example.md
http.md dev --watch example.md
```
This command will process `example.md`, execute the HTTP requests, and print the resulting markdown (with responses filled in) to the terminal. With `--watch`, any changes to `example.md` will trigger a re-run.
@@ -62,13 +74,13 @@ You have two primary ways to render your `http.md` file:
To generate a new markdown file with the responses and templated values rendered:
```shell
httpmd build example.md output.md
http.md build example.md output.md
```
With watch mode:
```shell
httpmd build --watch example.md output.md
http.md build --watch example.md output.md
```
This creates `output.md`, which is a static snapshot of `example.md` after all requests have been executed and templating applied. This file is suitable for version control, sharing, or integration with static site generators.
@@ -128,10 +140,17 @@ You can assign a unique ID to an `http` request block. This allows you to:
1. Reference its specific response in a `::response` directive.
2. Access its request and response data in [Templating](https://www.google.com/search?q=%23templating-with-handlebars) via the `requests` and `responses` dictionaries.
To add an ID, include `id=yourUniqueId` in the `http` block's info string:
To add an ID, include `#yourUniqueId` or `id=yourUniqueId` in the `http` block's info string:
::raw-md[./examples/with-multiple-requests.md]
<details>
<summary>Output</summary>
::raw-md[./examples/with-multiple-requests.md]{render}
</details>
## Templating with Handlebars
`http.md` uses [Handlebars](https://handlebarsjs.com/) for templating, allowing you to create dynamic content within your markdown files. You can inject data from request responses, input variables, and other requests into your HTTP blocks or general markdown text.
@@ -168,7 +187,7 @@ Within your markdown document, the following variables are available in the Hand
- **`input`** (Object): A dictionary of variables passed to `http.md` via the command line using the `-i` or `--input` flag.
- Example: If you run `httpmd dev -i userId=123 -i apiKey=secret myfile.md`, you can use `{{input.userId}}` and `{{input.apiKey}}`.
- Example: If you run `http.md dev -i userId=123 -i apiKey=secret myfile.md`, you can use `{{input.userId}}` and `{{input.apiKey}}`.
### Templating Examples
@@ -192,7 +211,7 @@ _(Note: `httpbin.org/post` wraps the JSON sent in a "json" field in its response
GET https://httpbin.org/status/201
```
The request to `/status/201` completed with status code: **{{response.status}}**.
The request to `/status/201` completed with status code: **{{{response.status}}}**.
````
## Managing Documents
@@ -209,34 +228,18 @@ The requests from the embedded document are processed, and their `request` and `
Assume `_shared_requests.md` contains:
````markdown
```http id=sharedGetRequest
GET https://httpbin.org/get
```
````
::raw-md[./examples/_shared_requests.md]
Then, in `main.md`:
````markdown
# Main Document
::raw-md[./examples/with-shared-requests.md]
Let's include some shared requests:
<details>
<summary>Output</summary>
::md[./_shared_requests.md]
::raw-md[./examples/with-shared-requests.md]{render}
The shared GET request returned: {{responses.sharedGetRequest.status}}
Now, a request specific to this document:
```http
POST https://httpbin.org/post
Content-Type: application/json
{"dataFromMain": "someValue", "sharedUrl": "{{requests.sharedGetRequest.url}}"}
```
::response
````
</details>
When `main.md` is processed, `_shared_requests.md` will be embedded, its `sharedGetRequest` will be executed, and its data will be available for templating.
@@ -249,15 +252,15 @@ You can pass external data into your `http.md` documents using the `-i` (or `--i
**CLI Command:**
```shell
httpmd build mydoc.md output.md -i baseUrl=https://api.production.example.com -i apiKey=YOUR_SECRET_KEY
http.md build mydoc.md output.md -i baseUrl=https://api.production.example.com -i apiKey=YOUR_SECRET_KEY
```
**Markdown Usage (`mydoc.md`):**
````markdown
```http
GET {{input.baseUrl}}/users/1
Authorization: Bearer {{input.apiKey}}
GET {{{input.baseUrl}}}/users/1
Authorization: Bearer {{{input.apiKey}}}
```
::response
@@ -265,6 +268,33 @@ Authorization: Bearer {{input.apiKey}}
**Security Note:** For sensitive data like API keys, using input variables is highly recommended over hardcoding them in your markdown files. Avoid committing files with plaintext secrets; instead, provide them at runtime via the CLI.
### JavaScript Execution
You can execute `javascript` blocks by adding a `run` option which allows programmatically changing the context, making request assertions and solve other more advanced use cases
**Example:**
::raw-md[./examples/with-javascript.md]
<details>
<summary>Output</summary>
::raw-md[./examples/with-javascript.md]{render}
</details>
**Options:**
- `run`: If present the code block will be executed
- Example: ` ```javascript run `
- `hidden`: If present the code block will not be included in the resulting output
- Example: ` ```javascript hidden `
- `output`: If present the code blocks return value will be rendered as a `yaml` code block
### HTTP Block Configuration Options
You can configure the behavior of each `http` code block by adding options to its info string, separated by commas.
@@ -306,8 +336,8 @@ You can configure the behavior of each `http` code block by adding options to it
````markdown
```http id=complexRequest,json,yaml,hidden
POST {{input.apiEndpoint}}/data
X-API-Key: {{input.apiKey}}
POST {{{input.apiEndpoint}}}/data
X-API-Key: {{{input.apiKey}}}
Content-Type: application/json
# Request body written in YAML, will be converted to JSON
@@ -358,9 +388,9 @@ The `::input` directive is used to declare expected input variables
## Command-Line Interface (CLI)
The `httpmd` tool provides the following commands:
The `http.md` tool provides the following commands:
### `httpmd dev <source_file.md>`
### `http.md dev <source_file.md>`
Processes the `<source_file.md>`, executes all HTTP requests, resolves templates, and prints the resulting markdown to the **terminal (stdout)**.
@@ -372,10 +402,10 @@ Processes the `<source_file.md>`, executes all HTTP requests, resolves templates
**Example:**
```shell
httpmd dev api_tests.md --watch -i host=localhost:3000
http.md dev api_tests.md --watch -i host=localhost:3000
```
### `httpmd build <source_file.md> <output_file.md>`
### `http.md build <source_file.md> <output_file.md>`
Processes the `<source_file.md>`, executes all HTTP requests, resolves templates, and saves the resulting markdown to `<output_file.md>`.
@@ -387,5 +417,5 @@ Processes the `<source_file.md>`, executes all HTTP requests, resolves templates
**Example:**
```shell
httpmd build official_api_docs.md public/api_docs_v1.md -i version=v1.0
http.md build official_api_docs.md public/api_docs_v1.md -i version=v1.0
```

View File

@@ -0,0 +1,3 @@
```http #sharedGetRequest
GET https://httpbin.org/get
```

View File

@@ -0,0 +1,17 @@
```javascript run
input.test = "Hello World";
```
::input[test]
```http json
POST https://httpbin.org/post
{"input": "{{input.test}}"}
```
```javascript run,hidden
// Use chai's `expect`, `assert` or `should` to make assumptions
expect(response.body.json.input).to.equal("Hello World");
```

View File

@@ -2,16 +2,16 @@
First, create a resource:
```http id=createUser
```http #createUser,yaml,json
POST https://httpbin.org/post
Content-Type: application/json
{"username": "alpha"}
username: alpha
```
Then, fetch a different resource:
```http id=getItem
```http #getItem
GET https://httpbin.org/get?item=123
```

View File

@@ -0,0 +1,18 @@
# Main Document
Let's include some shared requests:
::md[./_shared_requests.md]
The shared GET request returned: {{response.statusText}}
Now, a request specific to this document:
```http
POST https://httpbin.org/post
Content-Type: application/json
{"dataFromMain": "someValue", "sharedUrl": "{{requests.sharedGetRequest.url}}"}
```
::response

View File

@@ -1,11 +1,11 @@
{
"name": "@morten-olsen/httpmd",
"name": "@morten-olsen/http.md",
"version": "1.0.0",
"description": "",
"main": "dist/exports.js",
"type": "module",
"bin": {
"httpmd": "./bin/cli.mjs"
"http.md": "./bin/cli.mjs"
},
"files": [
"dist",
@@ -17,6 +17,7 @@
"build": "pnpm run build:lib && pnpm run build:readme",
"build:lib": "tsc --build",
"build:readme": "pnpm run cli build docs/README.md README.md",
"build:readme-html": "pnpm run cli build docs/README.md README.html -f html",
"dev:readme": "pnpm run cli dev docs/README.md --watch",
"test": "echo \"Error: no test specified\" && exit 1"
},
@@ -26,25 +27,34 @@
"devDependencies": {
"@pnpm/find-workspace-packages": "^6.0.9",
"@types/blessed": "^0.1.25",
"@types/chai": "^5.2.2",
"@types/ink": "^2.0.3",
"@types/marked-terminal": "^6.1.1",
"@types/mdast": "^4.0.4",
"@types/node": "^22.15.18",
"@types/react": "^18.3.12",
"@types/terminal-kit": "^2.5.7",
"tsx": "^4.19.4",
"typescript": "^5.8.3"
},
"dependencies": {
"blessed": "^0.1.81",
"chai": "^5.2.0",
"chalk": "^5.4.1",
"commander": "^14.0.0",
"dotenv": "^16.5.0",
"eventemitter3": "^5.0.1",
"handlebars": "^4.7.8",
"hastscript": "^9.0.1",
"ink": "^5.2.1",
"marked": "^15.0.11",
"marked-terminal": "^7.3.0",
"mdast-util-to-markdown": "^2.1.2",
"mdast-util-to-string": "^4.0.0",
"mdast-util-toc": "^7.1.0",
"react": "^18.3.1",
"rehype-stringify": "^10.0.1",
"remark-behead": "^3.1.0",
"remark-directive": "^4.0.0",
"remark-gfm": "^4.0.1",
"remark-parse": "^11.0.0",
@@ -53,6 +63,7 @@
"terminal-kit": "^3.1.2",
"unified": "^11.0.5",
"unist-util-visit": "^5.0.0",
"wrap-ansi": "^9.0.0",
"yaml": "^2.8.0"
}
}

524
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,33 +1,43 @@
import { program } from 'commander';
import { resolve } from 'node:path';
import { marked } from 'marked';
import { markedTerminal } from 'marked-terminal';
import { execute } from '../execution/execution.js';
import { Context } from '../context/context.js';
import { writeFile } from 'node:fs/promises';
import { Watcher } from '../watcher/watcher.js';
import { wrapBody } from '../theme/theme.html.js';
import { loadInputFiles } from '../utils/input.js';
import { InvalidFormatError } from '../utils/errors.js';
import { renderUI, State } from './ui/ui.js';
import { Marked } from 'marked';
marked.use(markedTerminal() as any);
program
.command('dev')
.argument('<name>', 'http.md file name')
.description('Run a http.md document')
.option('-w, --watch', 'watch for changes')
.option('-f, --file <file...>', 'input files (-f foo.js -f bar.json)')
.option('-i, --input <input...>', 'input variables (-i foo=bar -i baz=qux)')
.action(async (name, options) => {
const {
file: f = [],
watch = false,
input: i = [],
} = options;
const input = Object.fromEntries(
const input = {
...Object.fromEntries(
i.map((item: string) => {
const [key, value] = item.split('=');
return [key, value];
})
);
),
...loadInputFiles(f),
};
const state = new State<any>({
markdown: 'Loading',
});
const filePath = resolve(process.cwd(), name);
const build = async () => {
@@ -38,8 +48,10 @@ program
context,
});
const markdown = await marked.parse(result.markdown);
console.log(markdown);
state.setState({
error: result.error ? result.error instanceof Error ? result.error.message : result.error : undefined,
markdown: result.markdown,
});
return {
...result,
@@ -48,6 +60,7 @@ program
}
const result = await build();
renderUI(state);
if (watch) {
const watcher = new Watcher();
@@ -66,21 +79,28 @@ program
.argument('<name>', 'http.md file name')
.argument('<output>', 'output file name')
.description('Run a http.md document')
.option('-f, --file <file...>', 'input files (-f foo.js -f bar.json)')
.option('--format <format>', 'output format (html, markdown)')
.option('-w, --watch', 'watch for changes')
.option('-i, --input <input...>', 'input variables (-i foo=bar -i baz=qux)')
.action(async (name, output, options) => {
const {
watch = false,
file: f = [],
input: i = [],
format = 'markdown',
} = options;
const input = Object.fromEntries(
const input = {
...Object.fromEntries(
i.map((item: string) => {
const [key, value] = item.split('=');
return [key, value];
})
);
),
...loadInputFiles(f),
}
const filePath = resolve(process.cwd(), name);
const build = async () => {
@@ -91,7 +111,19 @@ program
context,
});
if (result.error) {
console.error(result.error);
}
if (format === 'html') {
const marked = new Marked();
const html = await marked.parse(result.markdown);
await writeFile(output, wrapBody(html));
} else if (format === 'markdown') {
await writeFile(output, result.markdown);
} else {
throw new InvalidFormatError('Invalid format');
}
return {
...result,
context,
@@ -100,6 +132,10 @@ program
const result = await build();
if (result.error && !watch) {
process.exit(1);
}
if (watch) {
const watcher = new Watcher();
watcher.watchFiles(Array.from(result.context.files));

View File

@@ -0,0 +1,55 @@
import React, { useMemo } from 'react';
import { Box, Text, useInput } from 'ink';
import { marked } from 'marked';
import TerminalRenderer from 'marked-terminal';
import wrapAnsi from 'wrap-ansi';
import os from 'os';
import { useTerminalHeight, useTerminalWidth } from '../hooks/terminal.js';
type ScrollableMarkdownProps = {
markdownContent: string;
};
const renderer = new TerminalRenderer({});
marked.setOptions({ renderer: renderer as any });
const ScrollableMarkdown = ({ markdownContent }: ScrollableMarkdownProps) => {
const [scrollPosition, setScrollPosition] = React.useState(0);
const terminalHeight = useTerminalHeight()
const terminalWidth = useTerminalWidth();
const rendered = useMemo(() => {
return marked(markdownContent) as string;
}, [markdownContent]);
const wrapped = useMemo(() => {
return wrapAnsi(rendered, terminalWidth, {
hard: true,
trim: false,
wordWrap: true,
}).split(os.EOL);
}, [rendered, terminalWidth]);
const buffer = useMemo(() => {
return wrapped.slice(scrollPosition, scrollPosition + terminalHeight).join(os.EOL);
}, [wrapped, scrollPosition, terminalHeight]);
useInput((input, key) => {
if (key.downArrow) {
setScrollPosition(prev => prev + 1);
}
if (key.upArrow) {
setScrollPosition(prev => Math.max(0, prev - 1));
}
if (key.escape) {
process.exit(0);
}
console.error('a', input, key);
});
return (
<Box flexDirection="column" flexShrink={1} flexGrow={1} overflow="hidden">
<Text>{buffer}</Text>
</Box>
);
};
export { ScrollableMarkdown };

View File

@@ -0,0 +1,39 @@
import { useEffect, useState } from "react"
const useTerminalWidth = () => {
const [width, setWidth] = useState(process.stdout.columns || 80);
useEffect(() => {
const handleResize = () => {
setWidth(process.stdout.columns);
};
process.stdout.on("resize", handleResize);
return () => {
process.stdout.off("resize", handleResize);
};
}, []);
return width;
}
const useTerminalHeight = () => {
const [height, setHeight] = useState(process.stdout.rows || 24);
useEffect(() => {
const handleResize = () => {
setHeight(process.stdout.rows);
};
process.stdout.on("resize", handleResize);
return () => {
process.stdout.off("resize", handleResize);
};
}, []);
return height;
}
export { useTerminalWidth, useTerminalHeight }

View File

@@ -0,0 +1,80 @@
import { EventEmitter } from 'eventemitter3';
import React, { createContext, useRef, useState, useSyncExternalStore } from "react";
type StateEvents = {
update: () => void;
}
class State<T = any> extends EventEmitter<StateEvents> {
state: T;
constructor(initialState: T) {
super();
this.state = initialState;
}
public get value() {
return this.state;
}
public setState = (state: T) => {
this.state = state;
this.emit('update');
}
public subscribe = (callback: () => void) => {
this.on('update', callback);
return () => {
this.off('update', callback);
};
}
}
type StateContextValue<T> = {
state: State<T>;
}
const StateContext = createContext<StateContextValue<any> | null>(null);
type StateProviderProps<T> = {
state: State<T>;
children: React.ReactNode;
}
const StateProvider = <T,>({ state, children }: StateProviderProps<T>) => {
return (
<StateContext.Provider value={{ state }}>
{children}
</StateContext.Provider>
);
};
const useStateContext = <T = any>() => {
const context = React.useContext(StateContext);
if (!context) {
throw new Error("useStateContext must be used within a StateProvider");
}
return context as StateContextValue<T>;
}
const useStateValue = <T = any>(
selector: (state: T) => any = (state) => state,
) => {
const context = useStateContext<T>();
const value = useRef<T>(selector(context.state.value));
useSyncExternalStore(
context.state.subscribe,
() => {
const next = selector(context.state.value);
if (next !== value.current) {
value.current = next;
}
return value.current;
},
() => value.current,
);
return value.current;
}
export { State, StateProvider, useStateContext, useStateValue };

69
src/cli/ui/ui.tsx Normal file
View File

@@ -0,0 +1,69 @@
import { Box, render, Text, useApp, useInput } from "ink"
import { ScrollableMarkdown } from './components/scrollable-markdown.js';
import { useStateValue, State, StateProvider } from './state/state.js';
import { useEffect, useState } from "react";
import { useTerminalHeight } from "./hooks/terminal.js";
const MarkdownView = () => {
const markdown = useStateValue((state) => state.markdown);
const error = useStateValue((state) => state.error);
return (
<Box flexGrow={1} flexDirection="column">
<Box flexDirection="column" flexGrow={1} overflow="hidden">
<ScrollableMarkdown markdownContent={markdown} />
</Box>
{error && (
<Box flexDirection="column" padding={1}>
<Text color="red">{error}</Text>
</Box>
)}
</Box>
);
}
const App = () => {
const { exit } = useApp();
const height = useTerminalHeight();
useInput((_input, key) => {
if (key.escape) {
process.exit(0);
}
});
useEffect(
() => {
const enterAltScreenCommand = "\x1b[?1049h";
const leaveAltScreenCommand = "\x1b[?1049l";
process.stdout.write(enterAltScreenCommand);
const onExit = () => {
exit();
process.stdout.write(leaveAltScreenCommand);
}
process.on("exit", onExit);
return () => {
process.stdout.write(leaveAltScreenCommand);
process.off("exit", onExit);
}
}, []
);
return (
<Box height={height} flexDirection="column">
<MarkdownView />
<Box>
<Text>Press esc to exit</Text>
</Box>
</Box>
)
}
const renderUI = (state: State) => {
render(
<StateProvider state={state}>
<App />
</StateProvider>
);
}
export { renderUI, State }

View File

@@ -10,6 +10,7 @@ type Response = {
statusText: string;
headers: Record<string, string>;
body?: string;
rawBody?: string;
};
type AddRequestOptios = {

View File

@@ -5,18 +5,12 @@ import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import remarkDirective from 'remark-directive'
import remarkStringify from 'remark-stringify'
import behead from 'remark-behead';
import { unified } from 'unified'
import { visit } from 'unist-util-visit'
import { Context } from "../context/context.js";
import { handlers } from './handlers/handlers.js';
const parser = unified()
.use(remarkParse)
.use(remarkGfm)
.use(remarkDirective)
.use(remarkStringify)
.use(remarkRehype);
import { handlers, postHandlers } from './handlers/handlers.js';
type BaseNode = {
type: string;
@@ -53,14 +47,26 @@ type ExecutionHandler = (options: {
type ExexutionExecuteOptions = {
context: Context;
behead?: number;
}
const execute = async (file: string, options: ExexutionExecuteOptions) => {
let error: unknown | undefined;
const { context } = options;
context.files.add(file);
const content = await readFile(file, 'utf-8');
const steps: Set<ExecutionStep> = new Set();
const parser = unified()
.use(remarkParse)
.use(remarkGfm)
.use(remarkDirective)
.use(remarkStringify)
.use(remarkRehype)
.use(behead, {
depth: options.behead,
});
const root = parser.parse(content);
visit(root, (node, index, parent) => {
@@ -75,8 +81,21 @@ const execute = async (file: string, options: ExexutionExecuteOptions) => {
});
}
});
visit(root, (node, index, parent) => {
for (const handler of postHandlers) {
handler({
addStep: (step) => steps.add(step),
node: node as BaseNode,
root,
parent: parent as BaseNode | undefined,
index,
file,
});
}
});
for (const step of steps) {
try {
const { node, action } = step;
const options: ExecutionStepOptions = {
file,
@@ -86,11 +105,16 @@ const execute = async (file: string, options: ExexutionExecuteOptions) => {
root,
};
await action(options);
} catch (e) {
error = e;
break;
}
}
const markdown = parser.stringify(root);
return {
error,
root,
markdown,
};

View File

@@ -5,7 +5,7 @@ const codeHandler: ExecutionHandler = ({
node,
addStep,
}) => {
if (node.type !== 'code' || node.lang === 'http') {
if (node.type !== 'code' || node.lang === 'http' || node.lang === 'javascript') {
return;
}
const optionParts = node.meta?.split(',') || [];

View File

@@ -44,7 +44,7 @@ const httpHandler: ExecutionHandler = ({
);
let parsedBody = body;
if (options.format === 'yaml') {
if (options.yaml) {
try {
const parsed = YAML.parse(body);
parsedBody = JSON.stringify(parsed);
@@ -59,7 +59,8 @@ const httpHandler: ExecutionHandler = ({
body
});
let responseText = await response.text();
const rawBody = await response.text();
let responseText = rawBody;
if (options.json) {
try {
responseText = JSON.parse(responseText);
@@ -68,7 +69,7 @@ const httpHandler: ExecutionHandler = ({
}
}
node.value = content;
node.value = [head, parsedBody].filter(Boolean).join('\n\n');
node.meta = undefined;
context.addRequest({
@@ -84,6 +85,7 @@ const httpHandler: ExecutionHandler = ({
statusText: response.statusText,
headers: Object.fromEntries(response.headers.entries()),
body: responseText,
rawBody: rawBody,
},
});
},

View File

@@ -1,5 +1,6 @@
import { toString } from 'mdast-util-to-string';
import { type ExecutionHandler } from '../execution.js';
import { ParsingError, RequiredError } from '../../utils/errors.js';
const inputHandler: ExecutionHandler = ({
addStep,
@@ -15,7 +16,7 @@ const inputHandler: ExecutionHandler = ({
const name = toString(node);
if (node.attributes?.required === '' && context.input[name] === undefined) {
throw new Error(`Input "${name}" is required`);
throw new RequiredError(name);
}
if (node.attributes?.default !== undefined && context.input[name] === undefined) {
@@ -27,7 +28,7 @@ const inputHandler: ExecutionHandler = ({
if (format === 'number') {
context.input[name] = Number(context.input[name]);
if (context.input[name] !== undefined && isNaN(Number(context.input[name]))) {
throw new Error(`Input "${name}" must be a number, but got "${context.input[name]}"`);
throw new ParsingError(`Input "${name}" must be a number, but got "${context.input[name]}"`);
}
}
if (format === 'boolean') {
@@ -40,14 +41,14 @@ const inputHandler: ExecutionHandler = ({
try {
context.input[name] = JSON.parse(String(context.input[name]));
} catch (error) {
throw new Error(`Input "${name}" must be a valid JSON, but got "${context.input[name]}"`);
throw new ParsingError(`Input "${name}" must be a valid JSON, but got "${context.input[name]}"`);
}
}
if (format === 'date') {
const date = new Date(context.input[name] as string);
if (isNaN(date.getTime())) {
throw new Error(`Input "${name}" must be a valid date, but got "${context.input[name]}"`);
throw new ParsingError(`Input "${name}" must be a valid date, but got "${context.input[name]}"`);
}
context.input[name] = date;
}

View File

@@ -0,0 +1,74 @@
import Handlebars from "handlebars";
import YAML from "yaml";
import { should, expect, assert } from 'chai';
import { ExecutionHandler } from "../execution.js";
import { ScriptError } from "../../utils/errors.js";
const javascriptHandler: ExecutionHandler = ({
node,
parent,
index,
addStep,
}) => {
if (node.type !== 'code' || node.lang !== 'javascript') {
return;
}
const optionParts = node.meta?.split(',') || [];
node.meta = undefined;
const options = Object.fromEntries(
optionParts.filter(Boolean).map((option) => {
const [key, value] = option.split('=');
return [key.trim(), value?.trim() || true];
})
);
addStep({
type: 'code',
node,
action: async ({ context }) => {
const template = Handlebars.compile(node.value);
const content = template(context);
node.value = content;
if (options['run'] === true) {
const api = {
assert,
should,
expect,
...context,
}
try {
// eslint-disable-next-line no-new-func
const asyncFunc = new Function(
...Object.keys(api),
`return (async () => { ${content} })()`
);
const result = await asyncFunc(...Object.values(api));
if (options.output === true && index !== undefined) {
if (result !== undefined) {
parent?.children?.splice(index + 1, 0, {
type: 'code',
lang: 'yaml',
value: YAML.stringify(result, null, 2),
meta: undefined,
});
}
}
} catch (error) {
if (index !== undefined) {
parent?.children?.splice(index + 1, 0, {
type: 'code',
value: `Error: ${error instanceof Error ? error.message : String(error)}`,
meta: undefined,
});
}
throw new ScriptError(error instanceof Error ? error.message : String(error))
}
}
if (options.hidden === true && parent && index !== undefined) {
parent.children?.splice(index, 1);
}
},
});
};
export { javascriptHandler };

View File

@@ -1,6 +1,8 @@
import { dirname, resolve } from 'path';
import { toString } from 'mdast-util-to-string'
import { execute, type ExecutionHandler } from '../execution.js';
import { FileNotFoundError } from '../../utils/errors.js';
import { existsSync } from 'fs';
const fileHandler: ExecutionHandler = ({
addStep,
@@ -18,11 +20,12 @@ const fileHandler: ExecutionHandler = ({
dirname(file),
toString(node)
);
if (!filePath) {
throw new Error('File path is required');
if (!existsSync(filePath)) {
throw new FileNotFoundError(filePath);
}
const { root: newRoot } = await execute(filePath, {
context,
behead: node.attributes?.behead ? parseInt(node.attributes.behead) : undefined,
});
if (!parent) {
throw new Error('Parent node is required');

View File

@@ -3,6 +3,8 @@ import { Context } from '../../context/context.js';
import { execute, type ExecutionHandler } from '../execution.js';
import { dirname, resolve } from 'path';
import { toString } from 'mdast-util-to-string';
import { FileNotFoundError } from '../../utils/errors.js';
import { existsSync } from 'fs';
const rawMdHandler: ExecutionHandler = ({
addStep,
@@ -20,6 +22,9 @@ const rawMdHandler: ExecutionHandler = ({
dirname(file),
toString(node),
);
if (!existsSync(name)) {
throw new FileNotFoundError(name);
}
const context = new Context({
input: {},
});

View File

@@ -53,6 +53,7 @@ const responseHandler: ExecutionHandler = ({
const codeNode = {
type: 'code',
lang: 'http',
value: responseContent,
};
if (!parent || !('children' in parent) || index === undefined) {

View File

@@ -0,0 +1,29 @@
import { toc } from 'mdast-util-toc';
import { type ExecutionHandler } from '../execution.js';
const tocHandler: ExecutionHandler = ({
addStep,
node,
root,
parent,
index,
}) => {
if (node.type === 'leafDirective' && node.name === 'toc') {
addStep({
type: 'toc',
node,
action: async () => {
const result = toc(root, {
tight: true,
minDepth: 2,
})
if (!parent || !parent.children || index === undefined) {
throw new Error('Parent node is not valid');
}
parent.children.splice(index, 1, result.map as any);
},
})
}
}
export { tocHandler };

View File

@@ -6,6 +6,8 @@ import { rawMdHandler } from "./handlers.raw-md.js";
import { responseHandler } from "./handlers.response.js";
import { textHandler } from "./handlers.text.js";
import { codeHandler } from "./handlers.code.js";
import { tocHandler } from "./handlers.toc.js";
import { javascriptHandler } from "./handlers.javascript.js";
const handlers = [
fileHandler,
@@ -15,6 +17,11 @@ const handlers = [
inputHandler,
rawMdHandler,
codeHandler,
javascriptHandler,
] satisfies ExecutionHandler[];
export { handlers };
const postHandlers = [
tocHandler,
] satisfies ExecutionHandler[];
export { handlers, postHandlers };

29
src/theme/theme.html.ts Normal file
View File

@@ -0,0 +1,29 @@
const wrapBody = (body: string) => {
return `<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.8.1/github-markdown.min.css" />
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
}
.markdown-body {
max-width: 800px;
padding: 20px;
margin: auto;
}
</style>
</head>
<body>
<article class="markdown-body">
${body}
</article>
</body>
</html>`;
};
export { wrapBody };

44
src/utils/errors.ts Normal file
View File

@@ -0,0 +1,44 @@
class BaseError extends Error {
constructor(message: string) {
super(message);
this.name = this.constructor.name;
}
}
class FileNotFoundError extends BaseError {
constructor(filePath: string) {
super(`File not found: ${filePath}`);
}
}
class InvalidFileError extends BaseError {
constructor(filePath: string) {
super(`Invalid file: ${filePath}`);
}
}
class InvalidFormatError extends BaseError {
constructor(format: string) {
super(`Invalid format: ${format}`);
}
}
class ScriptError extends BaseError {
constructor(message: string) {
super(`Script error: ${message}`);
}
}
class ParsingError extends BaseError {
constructor(message: string) {
super(`Parsing error: ${message}`);
}
}
class RequiredError extends BaseError {
constructor(name: string) {
super(`Required input "${name}" is missing`);
}
}
export { BaseError, FileNotFoundError, InvalidFileError, InvalidFormatError, ScriptError, ParsingError, RequiredError };

60
src/utils/input.ts Normal file
View File

@@ -0,0 +1,60 @@
import { extname } from "path";
import { FileNotFoundError, InvalidFileError } from "./errors.js";
import { existsSync } from "fs";
import YAML from "yaml";
import { readFile } from "fs/promises";
const loadJsonFile = async (filePath: string) => {
const content = await readFile(filePath, "utf-8");
return JSON.parse(content);
}
const loadYamlFile = async (filePath: string) => {
const content = await readFile(filePath, "utf-8");
return YAML.parse(content);
}
const loadJsFile = async (filePath: string) => {
const { default: content } = await import(filePath);
return content;
}
const loadInputFiles = async (filePaths: string[]) => {
let inputs: Record<string, unknown> = {};
for (const filePath of filePaths) {
const type = extname(filePath);
if (!existsSync(filePath)) {
throw new FileNotFoundError(filePath);
}
try {
switch (type) {
case ".json":
inputs = {
...inputs,
...(await loadJsonFile(filePath)),
};
break;
case ".yaml":
case ".yml":
inputs = {
...inputs,
...(await loadYamlFile(filePath)),
};
break;
case ".js":
inputs = {
...inputs,
...(await loadJsFile(filePath)),
};
break;
default:
throw new InvalidFileError(filePath);
}
} catch (error) {
throw new InvalidFileError(filePath);
}
}
}
export { loadInputFiles };

View File

@@ -10,7 +10,11 @@
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
"outDir": "dist"
"outDir": "dist",
"jsx": "react-jsx"
},
"include": ["src/**/*.ts"]
"include": [
"src/**/*.ts",
"src/cli/ui/ui.tsx"
]
}