Skip to content

Commit

Permalink
Chore/tests and lint (#9)
Browse files Browse the repository at this point in the history
- feat: Add tests and deno.jsonc
- feat: Add CI for tests
- fix: Remove blockquotes format on last line
- fix: Move ruler's new line outside colors.reset
- chore: Lint
  • Loading branch information
littletof authored Aug 5, 2022
1 parent d9fd762 commit 5336921
Show file tree
Hide file tree
Showing 30 changed files with 581 additions and 38 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: CI

on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
build:
name: ${{ matrix.config.kind }} ${{ matrix.config.os }}
runs-on: ${{ matrix.config.os }}
timeout-minutes: 5
strategy:
matrix:
config:
- os: macOS-latest
kind: test
- os: windows-latest
kind: test
- os: ubuntu-latest
kind: test

steps:
- name: Clone repository
uses: actions/checkout@v2

- name: Install deno
uses: denolib/setup-deno@master
with:
deno-version: 1.24.0

- name: Running tests
if: matrix.config.kind == 'test'
run: deno task test
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ Feedback and contributions are always welcome. Open an issue or a PR, or contact
- [x] remove dots from codeblock backgrounds
- [x] links with images
- [x] ```# Header with *italic*```
- [x] strikethrough, ~~-underline~~
- [ ] tests
- [ ] fmt, lint
- [x] strikethrough, ~~underline~~
- [x] basic tests
- [x] lint
- [ ] fmt
- [ ] Look into alternatives for the AST generation.
3 changes: 2 additions & 1 deletion cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ if(!Deno.args) {
case '-l':
text = Deno.readTextFileSync(Deno.args[1]);
break;
case '-r':
case '-r': {
const resp = await fetch(Deno.args[1]);
text = await resp.text();
break;
}
case '-s':
text = Deno.args[1];
break;
Expand Down
17 changes: 17 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"tasks": {
"example": "deno run --unstable example.ts",
"bundle:mdast": "deno bundle --no-check ./mdast_shimmed_dep.ts mdast-util-from-markdown@1_2_0-shimmed.js",
"docs:gen": "deno run -A --unstable ./docs/snapper.ts",
"test": "deno test --allow-read=./test"
},
"fmt": {
"files": {
"exclude": [
"./mdast-util-from-markdown@1_2_0-shimmed.js",
"./test/testCases",
"./test/exampleOutput.out"
]
}
}
}
3 changes: 2 additions & 1 deletion deps.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export * as colors from 'https://deno.land/std@0.82.0/fmt/colors.ts';
export * as colors from 'https://deno.land/std@0.105.0/fmt/colors.ts';

import { mdast, gfmStrikethroughFromMarkdown, gfmStrikethrough } from './mdast-util-from-markdown@1_2_0-shimmed.js';
export const strikethroughExt = gfmStrikethroughFromMarkdown;
export const strike = gfmStrikethrough;
// deno-lint-ignore no-explicit-any
export type mdastFromMarkdownFn = (markdown: string, encodig?: string, options?: {extensions?: any[], mdastExtensions?: any[]}) => any;
export const fromMarkdown: mdastFromMarkdownFn = mdast;
2 changes: 1 addition & 1 deletion docs/snapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { renderMarkdown } from "../mod.ts";
// 1. have deno Puppeteer installed
// 2. deno run -A --unstable .\docs\snapper.ts

let md = Deno.readTextFileSync('docs/docs.md');
const 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").replace(/_{25}/, '');
Expand Down
22 changes: 12 additions & 10 deletions example.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {renderMarkdown} from './mod.ts';

const demoText = `
export const demoText = `
# deno charmd
This is an example, to showcase https://github.com/littletof/charmd
Expand Down Expand Up @@ -179,12 +179,14 @@ Autoconverted link https://deno.land
`;

let md;
if(Deno.args[0]) {
md = Deno.readTextFileSync(Deno.args[0]);
} else {
md = demoText;
}

// renderMarkdown(md)
console.log(renderMarkdown(md));
if(import.meta.main) {
let md;
if(Deno.args[0]) {
md = Deno.readTextFileSync(Deno.args[0]);
} else {
md = demoText;
}

// renderMarkdown(md)
console.log(renderMarkdown(md));
}
6 changes: 3 additions & 3 deletions generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function generator(node: Node, parent: Node, options: Options): string |
case 'blockquote':
// gen childs
return node.children?.map((child: Node) => generator(child, node, options)?.split('\n')
.map((l: string) => colors.gray(colors.italic(l)))
.map((l: string, i, a) => (i != a.length-1) ? colors.gray(colors.italic(l)) : l)
// then add horizontal line to the start of all generated chidren lines, except last \n
.map((l,i,a) => (i != a.length-1 && l.trim() ? '┃ ' : '') + l).join('\n')).join('');

Expand All @@ -84,7 +84,7 @@ export function generator(node: Node, parent: Node, options: Options): string |
} else {
const icons = options.listIcons || ['-', '◦', '▪', '▸'];
const icon = icons[Math.min(node.listLevel!, icons.length-1)];
return generateList(i => colors.gray(`${icon} `));
return generateList(_i => colors.gray(`${icon} `));
}
}

Expand Down Expand Up @@ -142,4 +142,4 @@ export function generator(node: Node, parent: Node, options: Options): string |
// console.log({...node});
return node.value;
}
};
}
5 changes: 3 additions & 2 deletions mdast_shimmed_dep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

// Shim document
globalThis.document = {
createElement: (...data: any[]) => {
// deno-lint-ignore no-explicit-any
createElement: (..._data: any[]) => {
return new class {
set innerHTML(data: string) {this.textContent = data};
set innerHTML(data: string) {this.textContent = data}
textContent = '';
}();
}
Expand Down
2 changes: 2 additions & 0 deletions renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ export interface Extension {

export interface MdastOptions {
/** SyntaxExtension[] */
// deno-lint-ignore no-explicit-any
extensions?: any[];
/** MdastExtension[] */
// deno-lint-ignore no-explicit-any
mdastExtensions?: any[];
}

Expand Down
188 changes: 188 additions & 0 deletions test/exampleOutput.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
# deno charmd

This is an example, to showcase https://github.com/littletof/charmd

If you want to test the module with your own markdown, provide its  path  as an argument and make sure  --allow-read  is also provided.

 codeblock [bash] 
  
 deno run --allow-read https://raw.githubusercontent.com/littletof/charmd/master/mod.ts ./README.md 
  

## Headers

# h1 Heading

# h1 Heading with  === 

## h2 Heading

## h2 Heading with  --- 

### h3 Heading

#### h4 Heading

##### h5 Heading

###### h6 Heading

________________________________________________________________________________

## Emphasis

This is bold text  ** 

This is bold text  __ 

This is italic text  * 

This is italic text  _ 

This is strikethrough  ~~ 

an italic in bold

an italic in bold with  striketrough 

## Blockquotes

┃ Blockquotes can also be nested...
┃ ┃ ...by using additional greater-than signs right next to each other...
┃ ┃ ┃ ...or with spaces between arrows.

┃ Some quote
┃ and more

┃ ┃ Another quote

┃ Some quote
┃ with bold and  block 
┃ ┃ and Another quote
┃ ┃ ┃ ┃ #### and some more quote

## Lists

### Unordered

- Create a list by starting a line with  + ,  - , or  * 
◦ Indented list item:
▪ Subsublist item
▸ Subsublist item
- Main list item

### Ordered

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

### Task List

- [x] Some
- [ ] Other

## Code

Inline  code   italics   bold   both 

Indented code

 codeblock  
  
 // Some comments 
 line 1 of code 
 line 2 of code 
 line 3 of code 
  

Block code "fences"

 codeblock  
  
 Sample text here... 
  

Codeblock with lang

 codeblock [ts] 
  
 function sum(...nums: number[]): number { 
  return nums.reduce((sum, num) => sum + num, 0); 
 } 
  

## Horizontal Rules

________________________________________________________________________________

________________________________________________________________________________

________________________________________________________________________________

________________________________________________________________________________

________________________________________________________________________________

________________________________________________________________________________

## Tables

┌────────┬────────┬────────┐
│ a │ b │ c │
├────────┼────────┼────────┤
│ 123 │ 456 │ 789 │
├────────┼────────┼────────┤
│ ABCDEF │ DEFGHI │ GHIJKL │
└────────┴────────┴────────┘

┌───────┬───────┬────────┬────────────────────────────────────┐
│ left1 │ left2 │ center │ right │
├───────┼───────┼────────┼────────────────────────────────────┤
│ 123 │ 456 │ 789 │ 101 │
├───────┼───────┼────────┼────────────────────────────────────┤
│  ABC  │ DEF │ GHI │ Image: ![image](docs/showcase.png) │
└───────┴───────┴────────┴────────────────────────────────────┘

┃ ┌────────┬────────┬────────┐
┃ │ a │ b │ c │
┃ ├────────┼────────┼────────┤
┃ │ 123 │ 456 │ 789 │
┃ ├────────┼────────┼────────┤
┃ │ ABCDEF │ DEFGHI │ GHIJKL │
┃ └────────┴────────┴────────┘

## Links

[Deno](https://deno.land)
[Bottom](#bottom)
[LinkReference]

[LinkReference]: [](https://deno.land/x)

[link with title](https://deno.land)
Autoconverted link https://deno.land

## Images

Image: ![Deno](https://deno.land/logo.svg)
Image: ![Deno](https://deno.land/logo.svg)

Image reference: ![Alt text][id]

[id]: [Deno logo](https://deno.land/logo.svg)

## HTML

<h2>HTML</h2>
<hr />
<div>Some `&lt;div&gt;` tag</div>
<strong>and **<strong>** tag</strong>
<br />
<footer align="center" id="bottom">
<a href="#headers-">
<strong>Top</strong>
<i class="fas fa-chevron-up"></i>
</a>
</footer>
________________________________________________________________________________
Loading

0 comments on commit 5336921

Please sign in to comment.