Skip to content

Commit

Permalink
fix: filter out HTML (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsanders11 authored Jun 21, 2024
1 parent d95a2a2 commit 43b5393
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/DocsParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export class DocsParser {
| ElementDocumentationContainer
)[] = [];
const contents = await fs.readFile(filePath, 'utf8');
const md = new MarkdownIt();
const md = new MarkdownIt({ html: true });

const allTokens = md.parse(contents, {});

Expand Down Expand Up @@ -263,7 +263,7 @@ export class DocsParser {

private async parseStructure(filePath: string): Promise<StructureDocumentationContainer> {
const contents = await fs.readFile(filePath, 'utf8');
const md = new MarkdownIt();
const md = new MarkdownIt({ html: true });

const tokens = md.parse(contents, {});
const baseInfos = await this.parseBaseContainers(filePath, contents, tokens);
Expand Down
11 changes: 11 additions & 0 deletions src/__tests__/__snapshots__/markdown-helpers.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,17 @@ exports[`markdown-helpers safelyJoinTokens snapshots should be correct for parag
Hey"
`;

exports[`markdown-helpers safelyJoinTokens snapshots should be correct for paragraph-with-br-tag 1`] = `
"Process: Main
_This class is not exported from the \`'electron'\` module. It is only available as a return value of other methods in the Electron API._
Process: Main
_This class is not exported from the \`'electron'\` module. It is only available as a return value of other methods in the Electron API._
Process: Main
_This class is not exported from the \`'electron'\` module. It is only available as a return value of other methods in the Electron API._"
`;

exports[`markdown-helpers safelyJoinTokens snapshots should be correct for paragraph-with-inline-code 1`] = `"This is a \`inline code\` block that is \`code\` tagged."`;

exports[`markdown-helpers safelyJoinTokens snapshots should be correct for paragraph-with-links 1`] = `"This paragraph has a bunch of stuff. Also some links Foo, these links should be stripped."`;
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/block-parsers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { parseMethodBlocks } from '../block-parsers';

describe('block parsers', () => {
it('should parse a method', async () => {
const md = new MarkdownIt();
const md = new MarkdownIt({ html: true });
const contents = `
# \`test.foo(x)\`
* \`x\` Integer - x
Expand Down
8 changes: 8 additions & 0 deletions src/__tests__/fixtures/paragraph-with-br-tag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Process: [Main](../glossary.md#main-process)<br />
_This class is not exported from the `'electron'` module. It is only available as a return value of other methods in the Electron API._

Process: [Main](../glossary.md#main-process)<br/>
_This class is not exported from the `'electron'` module. It is only available as a return value of other methods in the Electron API._

Process: [Main](../glossary.md#main-process)<br>
_This class is not exported from the `'electron'` module. It is only available as a return value of other methods in the Electron API._
2 changes: 1 addition & 1 deletion src/__tests__/markdown-helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { DocumentationTag } from '../ParsedDocumentation';

const getTokens = (md: string) => {
const markdown = new MarkdownIt();
const markdown = new MarkdownIt({ html: true });
return markdown.parse(md, {});
};

Expand Down
9 changes: 9 additions & 0 deletions src/markdown-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,8 @@ export const safelyJoinTokens = (tokens: Token[], options: JoinTokenOptions = {}
's_close',
'blockquote_open',
'blockquote_close',
'html_block',
'html_inline',
],
'We only support plain text, links, softbreaks, inline code, strong tags and paragraphs inside joinable tokens',
);
Expand Down Expand Up @@ -640,6 +642,13 @@ export const safelyJoinTokens = (tokens: Token[], options: JoinTokenOptions = {}
break;
case 'paragraph_open':
case 'blockquote_close':
case 'html_block':
break;
case 'html_inline':
// Replace <br> elements with a newline
if (tokenToCheck.content.match(/<br\s*\/?>/)) {
joinedContent += '\n';
}
break;
case 'fence':
if (options.parseCodeFences) {
Expand Down

0 comments on commit 43b5393

Please sign in to comment.