Skip to content

Commit

Permalink
feat: Add strikethrough (#8)
Browse files Browse the repository at this point in the history
- feat: Add strikethrough support with mdast-util-gfm-strikethrough
- chore: Update mdast-util-from-markdown to 1.2.0
- chore: Update snapper to 0.0.6
- refactor: some code formatting
  • Loading branch information
littletof authored Aug 5, 2022
1 parent 4392233 commit d9fd762
Show file tree
Hide file tree
Showing 12 changed files with 4,856 additions and 4,813 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,13 @@ The module itself requires no permissions to run.
## Limitations

- No syntax highlight
- No \~\~strikethrough\~\~ or underline
- No multiline tables cells
- Possible hiccups with more complex markdowns
- Possible hiccups with more complex markdowns (Please open an issue about it)

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.
>
> [syntax highlight example](https://github.com/littletof/charmd/issues/2#issuecomment-832771746)
## Notes

Expand All @@ -123,7 +124,7 @@ 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
- [ ] strikethrough, underline and combinations
- [ ] Look into alternatives for the AST generation.
9 changes: 5 additions & 4 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * as colors from 'https://deno.land/[email protected]/fmt/colors.ts';

// 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 as mdastFromMarkdownFn;
import { mdast, gfmStrikethroughFromMarkdown, gfmStrikethrough } from './mdast-util-from-markdown@1_2_0-shimmed.js';
export const strikethroughExt = gfmStrikethroughFromMarkdown;
export const strike = gfmStrikethrough;
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/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
###### h6 Heading
## `inline` *italic*

**bold** __text__ *italic* _text_ ***bold and italic***
**bold** __text__ *italic* _text_ ***bold and italic*** ~~striketrough~~

> Some quote with **bold** and `block`
> > and Another quote
Expand Down
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.
2 changes: 1 addition & 1 deletion docs/snapper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {snap} from 'https://deno.land/x/[email protected].5/mod.ts';
import {snap} from 'https://deno.land/x/[email protected].6/mod.ts';
import { renderMarkdown } from "../mod.ts";

// Usage:
Expand Down
11 changes: 7 additions & 4 deletions example.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { colors } from "./deps.ts";
import {renderMarkdown} from './mod.ts';

const demoText = `
# deno terminal markdown
# deno charmd
This is an example, to showcase https://github.com/littletof/terminal_markdown
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.
\`\`\`bash
deno run --allow-read https://raw.githubusercontent.com/littletof/terminal_markdown/master/mod.ts .\/README.md
deno run --allow-read https://raw.githubusercontent.com/littletof/charmd/master/mod.ts .\/README.md
\`\`\`
## Headers
Expand Down Expand Up @@ -40,8 +39,12 @@ __This is bold text \`\_\_\`__
_This is italic text \`\_\`_
~~This is strikethrough~~ \`\~\~\`
**an *italic* in bold**
~~**an *italic* in bold with \`striketrough\`**~~
## Blockquotes
> Blockquotes can also be nested...
Expand Down
37 changes: 25 additions & 12 deletions generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,37 @@ export function generator(node: Node, parent: Node, options: Options): string |
case 'root':
return node.children?.map((child: Node) => generator(child, node, options)).join('\n');

case 'definition':
case 'definition': {
const def = `${colors.cyan(`[${node.label}]`)}: [${node.title ?? ''}](${node.url})`;
return colors.gray(colors.italic(`${def}\n`));
case 'image':
}
case 'image': {
const imageLink = `![${node.alt}](${node.url})`;
return colors.gray(colors.italic(`Image: ${imageLink}`));
case 'imageReference':
}
case 'imageReference': {
const ref = `![${node.alt}]${colors.cyan(`[${node.label}]`)}`;
return colors.gray(colors.italic(`Image reference: ${ref}`));
case 'link':
}
case 'link': {
const linkText = node.children?.map(ch => generator(ch, node, options)).join('');
const link = `[${linkText}](${node.url})`
const link = `[${linkText}](${node.url})`;
return colors.cyan(link);
case 'strong':
}
case 'strong': {
const strongContent = node.children?.map((child: Node) => generator(child, node, options)).join('');
return colors.bold(strongContent || '');
case 'emphasis':
}
case 'emphasis': {
const emphasisContent = node.children?.map((child: Node) => generator(child, node, options)).join('');
return colors.italic(emphasisContent || '');
}
case 'delete': {
const strikeTroughContent = node.children?.map((child: Node) => generator(child, node, options)).join('');
return colors.strikethrough(strikeTroughContent || '');
}
case 'heading':
return getHeaderFormatter(node.depth || 0)('#'.repeat(node.depth!) + ' ' + node.children?.map((ch: Node) => generator(ch, node, options)).join('')) + '\n'
return getHeaderFormatter(node.depth || 0)('#'.repeat(node.depth!) + ' ' + node.children?.map((ch: Node) => generator(ch, node, options)).join('')) + '\n';

case 'linkReference':
return (
Expand All @@ -59,13 +69,13 @@ export function generator(node: Node, parent: Node, options: Options): string |
// 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('');

case 'list':
case 'list': {
const generateList = (icon: (i: number) => string) => {
const tabForList = ' ';
return node.children?.map((child: Node, i: number) => {
const tabForMultiline = ' '.repeat(colors.stripColor(icon(i)).length || 2);
return (icon(i) + generator(child, node, options))
?.replace(/\n$/, '').split('\n').map((l,i) => tabForList + (i ? tabForMultiline +l: l)).join('\n') + '\n'; // indent full list
?.replace(/\n$/, '').split('\n').map((l,i) => tabForList + (i ? tabForMultiline +l: l)).join('\n') + '\n'; // indent full list
}).join('').split('\n').map((l: string) => l.replace(tabForList, '')).join('\n'); // remove outermost indent from each line -> level 0 ha 0 indent
};

Expand All @@ -76,11 +86,12 @@ export function generator(node: Node, parent: Node, options: Options): string |
const icon = icons[Math.min(node.listLevel!, icons.length-1)];
return generateList(i => colors.gray(`${icon} `));
}
}

case 'listItem':
return node.children?.map((ch: Node) => generator(ch, parent, options)).join(node.spread? '\n' : '');

case 'code':
case 'code': {
let codeBlock = '';
const xPadding = 2;
const padString = (length: number) => colors.bgBrightBlack(colors.gray(' '.repeat(length)));
Expand All @@ -107,16 +118,18 @@ export function generator(node: Node, parent: Node, options: Options): string |
codeBlock += colors.bgBrightBlack(' ')+padString(max + 2*xPadding-1) + "\n";

return codeBlock;
}

case 'inlineCode':
// return colors.inverse(colors.bgBrightWhite(colors.black(` ${node.value} `)));
// return colors.white(colors.inverse(colors.bgBrightBlack(` ${node.value} `))); works
// return colors.white(colors.inverse(colors.bgBlack(` ${node.value} `))); // best
return colors.inverse(` ${node.value} `);

case 'table':
case 'table': {
const t = node.children?.map((child: Node) => generator(child, node, options)).join('') || '';
return generateTable(t, options.tableBorder ?? true) + '\n';
}

case 'thematicBreak':
return node.value;
Expand Down
Loading

0 comments on commit d9fd762

Please sign in to comment.