Skip to content

Commit

Permalink
feat: Support GitHub Flavored Markdown (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S authored Jan 28, 2023
1 parent bb7ed00 commit 71b9042
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 6 deletions.
10 changes: 10 additions & 0 deletions fixtures/code/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,13 @@ It is also possible to inject markdown:
```

<!--- @@inject: https://github.com/streetsidesoftware/inject-markdown/blob/d7de2f5fe/src/app.mts#L15-L19 --->

## GitHub Footnote

| | version | Node | Support | End-Of-Life |
| :----- | :------ | :--- | :-------------------- | :---------- |
| cspell | 6.x | 14.x | In Active Development | TBD |
| cspell | 5.x | 12.x | Paid support only[^1] | 2022-10-01 |
| cspell | 4.x | 10.x | Paid support only[^1] | 2022-05-01 |

[^1]: [Support - Street Side Software](https://streetsidesoftware.com/support/#maintenance-agreements)
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"globby": "^13.1.3",
"node-fetch": "^3.3.0",
"remark": "^14.0.2",
"remark-gfm": "^3.0.1",
"remark-mdx": "^2.2.1",
"remark-parse": "^10.0.1",
"remark-stringify": "^10.0.2",
Expand Down
160 changes: 160 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions src/FileInjector/FileInjector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import * as path from 'node:path';
import assert from 'assert';
import chalk, { supportsColor } from 'chalk';
import type { Code, Content, Heading, HTML, Parent, Root } from 'mdast';
import remarkGfm from 'remark-gfm';
import remarkParse from 'remark-parse';
import remarkStringify from 'remark-stringify';
import { unified } from 'unified';
import { type Processor, unified } from 'unified';
import { is } from 'unist-util-is';
import { remove } from 'unist-util-remove';
import { visit } from 'unist-util-visit';
Expand Down Expand Up @@ -262,8 +263,7 @@ async function processFileInjections(
file.data.hasInjections = false;
return file;
}
const result = await unified()
.use(remarkParse)
const result = await initParser()
.use(processHasInjections)
.use(processInjections)
.use(remarkStringify, outputOptions)
Expand Down Expand Up @@ -403,7 +403,7 @@ async function processFileInjections(
}

function parseMarkdownFile(file: VFileEx): Root {
return unified().use(remarkParse).parse(file);
return initParser().parse(file);
}

function relativePathNormalized(path: URL, relDir?: URL): string {
Expand Down Expand Up @@ -675,7 +675,7 @@ function toError(e: unknown): Error {

function errorToComment(err: Error): Root {
const msg = (err.message || err.toString()).split('\n').join('\n ');
return unified().use(remarkParse).parse(`\
return initParser().parse(`\
<!---
${msg}
--->`);
Expand All @@ -699,3 +699,7 @@ function extractLines(content: string, lines: [number, number] | undefined): str
function refersToTheSameFile(a: RelURL | URL, b: RelURL | URL): boolean {
return a.pathname === b.pathname && a.search === b.search;
}

function initParser(): Processor<Root, Root, Root, void> {
return unified().use(remarkParse).use(remarkGfm);
}
9 changes: 9 additions & 0 deletions src/FileInjector/VFileEx.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, expect, test } from 'vitest';

import * as VFileEx from './VFileEx.js';

describe('VFileEx', () => {
test('VFileEx', () => {
expect(VFileEx).toBeDefined();
});
});
2 changes: 1 addition & 1 deletion src/FileInjector/VFileEx.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Data as VFileData, VFile } from 'vfile';

import { BufferEncoding } from '../FileSystemAdapter/FileSystemAdapter.js';
import type { BufferEncoding } from '../FileSystemAdapter/FileSystemAdapter.js';

export interface FileData extends VFileData {
encoding: BufferEncoding;
Expand Down
10 changes: 10 additions & 0 deletions src/FileInjector/__snapshots__/FileInjector.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ async function version(): Promise<string> {
\`\`\`
<!--- @@inject-end: https://github.com/streetsidesoftware/inject-markdown/blob/d7de2f5fe/src/app.mts#L15-L19 --->
## GitHub Footnote
| | version | Node | Support | End-Of-Life |
| :----- | :------ | :--- | :-------------------- | :---------- |
| cspell | 6.x | 14.x | In Active Development | TBD |
| cspell | 5.x | 12.x | Paid support only[^1] | 2022-10-01 |
| cspell | 4.x | 10.x | Paid support only[^1] | 2022-05-01 |
[^1]: [Support - Street Side Software](https://streetsidesoftware.com/support/#maintenance-agreements)
",
"utf8",
],
Expand Down

0 comments on commit 71b9042

Please sign in to comment.