Skip to content

Commit

Permalink
Implement main functionality for first release (#6)
Browse files Browse the repository at this point in the history
- use `mdast` as a bundled dep, instead of from jspm
- Adds extensions
- Refactors inner workings
- Refactor rendering for many node types
- Improve basic rendering
- Fix bugs
  • Loading branch information
littletof authored Feb 1, 2021
1 parent 684abcb commit e5f8dc9
Show file tree
Hide file tree
Showing 14 changed files with 5,155 additions and 336 deletions.
85 changes: 64 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
# Deno terminal markdown
# 🎩 charMD

> 🚧 This module is in early developement, expect breaking changes 🚧
> Also not yet released in any repository. Until than, you can use it with the raw urls
Enables you, to render a markdown text into a string, which printed in the terminal provides a formatted output, instead of plain text.
**`charMD`** enables you to render a markdown text into a string, which printed in the terminal provides a well formatted output, instead of plain text.

![Showcase](docs/showcase.png)

As its core, it currently uses <https://github.com/syntax-tree/mdast-util-from-markdown>, to get the `AST`.

This module draws heavily from @dephraims work with <https://github.com/dephraiim/termd>.

## Try it out

To see a general capabilities of this module run:
To see the general capabilities of this module run:

```bash
deno run https://raw.githubusercontent.com/littletof/terminal_markdown/master/example.ts
Expand All @@ -28,18 +22,47 @@ deno run --allow-read https://raw.githubusercontent.com/littletof/terminal_markd

## Usage

Simply import the module and call the `renderMarkdown` method with your markdown text.

```ts
import { renderMarkdown } from 'https://raw.githubusercontent.com/littletof/terminal_markdown/master/mod.ts';

const md = Deno.readTextFileSync(Deno.args[0]);
console.log(renderMarkdown(md));
console.log(renderMarkdown('# Hello world 🌍!'));
```

The main functions are also exported from the module, so you can use the `toAST` function to get your ast for the markdown and process it yourself.
### 🧩 Extensions

The module provides a way to extend it functionality with additional extensions, which can be provided in it's `options` param.

An extension can implement any of the `Extension` interface's methods, which are:

- `init`: Called before AST generation, if a string is returned, it will override the input markdown for later extension's init fn and processing steps.
- `postAST`: Called with the generated AST's root node, before any transformations
- `transformNode`: Called with each node to do modifications on the node and it's children in the `AST`.
- `postTransform`: Called with root, after all the transformations ran for all nodes.
- `generateNode`: Called with each node. It should return the string representation of the rendered node,if the extension handles that specific node, or void, if its not handled by the extension.
- `postGenerate`: Called after the string representation is created.

A simple extension, that renders `inlineCode` with bold green instead of the built-in gray italic would look something like this:

Also, since its a wrapper for [mdast-util-from-markdown](https://github.com/syntax-tree/mdast-util-from-markdown), you can pass `extensions` to it in the options, which should work too.
```ts
const LinkExt = {
generateNode(genFn, node: Node, parent: Node, options: Options) {
if(node.type === 'link') {
const linkText = node.children?.map(ch => genFn(ch, node, options)).join('') || '';
const link = `Link with text '${colors.blue(linkText)}' points to ${colors.cyan(node.url!)}`
return colors.green(link);
}
}
}

console.log(renderMarkdown(
'[charMD](https://github.com/littletof/charmd)',
{ extensions: [LinkExt] }
));
```

### Direct use
### Direct use - `cli.ts`

For direct use in the terminal run `cli.ts`:

Expand All @@ -59,23 +82,43 @@ It has three options:

The module itself requires no permissions to run.

> If `--unstable` is provided, horizontal separators will consider the terminal's width.
<!--
|Flag| Required |Reason|
|:--|:-:|:--|
| 🚧 `--unstable` | | If provided, the horizontal separator will consider the terminals width |
-->

## Limitations

- No syntax highlight
- No \~\~strikethrough\~\~ or underline
- Some hiccups with more complex markdowns
- No multiline tables cells
- Possible hiccups with more complex markdowns

These could change in the future, but the aim is to keep the module's complexity minimal.
> Also, many of these should also be solvable using extensions.
## Notes

- The main functions are also exported from the module, so for example you can use the `toAST` function to get your ast for the markdown and process it yourself.

- As its core, it currently uses [mdast-util-from-markdown](https://github.com/syntax-tree/mdast-util-from-markdown), to get the `AST`, you can also provide extensions to it in the options **unstable** `mdast` property.

These could change in the future, but the aim is to keep minimal complexity.
- This module's core structure draws from @dephraims work with <https://github.com/dephraiim/termd>.

## Contributions

Feedback and contributions are always welcome. Open an issue or a PR, or contact me on the Deno discord.

## TODO

- [ ] fix lists
- [ ] remove dots from codeblock backgrounds
- [ ] strikethrough and underline and combinations
- [ ] links with images
- [ ] ```# Header with *italic*```
- [x] fix lists
- [x] remove dots from codeblock backgrounds
- [x] links with images
- [x] ```# Header with *italic*```
- [ ] tests
- [ ] fmt, lint
- [ ] strikethrough, underline and combinations
- [ ] Look into alternatives for the AST generation.
5 changes: 3 additions & 2 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * as colors from 'https://deno.land/[email protected]/fmt/colors.ts';

import * as mdast from 'https://jspm.dev/[email protected]';
// import * as mdast from 'https://jspm.dev/[email protected]';
import {mdast} from './mdast-util-from-markdown@0_8_4-shimmed.js';
export type mdastFromMarkdownFn = (markdown: string, encodig?: any, options?: {extensions?: any[], mdastExtensions?: any[]}) => any;
export const fromMarkdown: mdastFromMarkdownFn = mdast.default as mdastFromMarkdownFn;
export const fromMarkdown: mdastFromMarkdownFn = mdast as mdastFromMarkdownFn;
25 changes: 14 additions & 11 deletions docs/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@
#### h4 Heading
##### h5 Heading
###### h6 Heading
## `inline` *italic*

**bold text**
*italic text*
**bold** __text__ *italic* _text_ ***bold and italic***

> Some quote with **bold** and `block`
> > and Another quote
>> >> ### and some more quote
* ListItem
+ Indented list item:
* ListItem
* Lists support
+ indentation
- and
- `*`, `+`, `-`
- ordered listItems
1. first
2. second
- multi
line
content

1. Lorem ipsum dolor sit amet
2. Consectetur adipiscing elit
3. Integer molestie lorem at massa

Inline `code`
Inline `code` *`italics`* **`bold`** ***`both`***

```ts
function sum(...nums: number[]): number {
Expand All @@ -35,7 +38,7 @@ function sum(...nums: number[]): number {
| 123 | 456 | 789 |
| ABCDEF | DEFGHI | GHIJKL |

[Deno](https://deno.land)
[charMD](https://github.com/littletof/charmd)
[LinkReference]

[LinkReference]: https://deno.land/x
Expand Down
7 changes: 7 additions & 0 deletions docs/docs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {renderMarkdown} from '../mod.ts';

let md = Deno.readTextFileSync('docs/docs.md');
let r = renderMarkdown(md);
// remove empty lines after header, to save space on image
r = r.replace(/\n\n/, "\n").replace(/\n\n/, "\n").replace(/\n\n/, "\n").replace(/\n\n/, "\n").replace(/\n\n/, "\n").replace(/\n\n/, "\n");
console.log(r);
Binary file modified docs/showcase.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 13 additions & 32 deletions example.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { colors } from "./deps.ts";
import {renderMarkdown} from './mod.ts';

const demoText = `
Expand All @@ -14,12 +15,12 @@ deno run --allow-read https://raw.githubusercontent.com/littletof/terminal_markd
# h1 Heading
h1 Heading with ===
h1 Heading with \`===\`
===
## h2 Heading
h2 Heading with ---
h2 Heading with \`---\`
---
### h3 Heading
Expand All @@ -41,8 +42,6 @@ _This is italic text \`\_\`_
**an *italic* in bold**
> 🔺 no strikethrough or underline, and some combinations doesnt work properly currently
## Blockquotes
> Blockquotes can also be nested...
Expand All @@ -61,14 +60,12 @@ and more
## Lists
> 🔺 problems with multitabbed lists, extra list numbering and list following another one
### Unordered
+ Create a list by starting a line with \`+\`, \`-\`, or \`*\`
- Indented list item:
* Subsublist item
+ Subsublist item
+ Subsublist item
+ Main list item
### Ordered
Expand All @@ -85,7 +82,7 @@ and more
## Code
Inline \`code\`
Inline \`code\` *\`italics\`* **\`bold\`** ***\`both\`***
Indented code
Expand All @@ -101,18 +98,12 @@ Block code "fences"
Sample text here...
\`\`\`
No syntax highlighting
Codeblock with lang
\`\`\`ts
function sum(...nums: number[]): number {
return nums.reduce((sum, num) => sum + num, 0);
}
let a: number = 1;
let b: number = 2;
let result: number = sum(a, b);
console.log(\`\${ a } + \${ b } = \${ result }\`);
\`\`\`
## Horizontal Rules
Expand All @@ -134,27 +125,17 @@ ___
| a | b | c |
| ----- | :---: | ----: |
| 123 | 456 | 789 |
| ABC | DEF | GHI |
| ABCDEF | DEFGHI | GHIJKL |
| left1 |left2| center | right |
|-------|:----|:-----:|----:|
| 123 | 456 | 789 | 101 |
| \`ABC\` | **DEF** | *GHI* | JKL |
> | Option | Description |
> | ------ | ----------- |
> | data | path to data files to supply the </br> data that will be passed into templates. |
> | engine | engine to be used for processing templates. Handlebars is the default. |
> | ext | extension to be used for dest files. |
> | image | ![image](https://deno.land/logo.svg) |
| Option | Description |
| ------:| -----------:|
| data | path to data files to supply the data that will be passed into templates. |
| engine | engine to be used for processing templates. Handlebars is the default. |
| ext | extension to be used for dest files. |
| image | ![image](https://deno.land/logo.svg) |
| \`ABC\` | **DEF** | *GHI* | ![image](docs/showcase.png) |
> | a | b | c |
> | ----- | :---: | ----: |
> | 123 | 456 | 789 |
> | ABCDEF | DEFGHI | GHIJKL |
## Links
Expand Down
Loading

0 comments on commit e5f8dc9

Please sign in to comment.