Skip to content
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
401 changes: 327 additions & 74 deletions __tests__/compilers/html-block.test.ts
Comment thread
maximilianfalco marked this conversation as resolved.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More tests suggestion:

  • HTMLBlock that spans multiple lines and have text before & after it, and having some random amount of empty spaces in between. E.g.
Hello <HTMLBlock>{\`
<p><strong">Hello</strong>, World!</p>

\`}</HTMLBlock>

there
  • Nested HTMLBlocks
<HTMLBlock>{`<HTMLBlock>{inner}</HTMLBlock>`}</HTMLBlock>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More tests suggestion:

  • HTMLBlock that spans multiple lines and have text before & after it, and having some random amount of empty spaces in between. E.g.
Hello <HTMLBlock>{\`
<p><strong">Hello</strong>, World!</p>

\`}</HTMLBlock>

there

i dont think we should cover this case tho, even MDX themselve dont support this so i think this is just plain malformed and wrong syntax

  • Nested HTMLBlocks
<HTMLBlock>{`<HTMLBlock>{inner}</HTMLBlock>`}</HTMLBlock>

added this one with a bit of a tweak!

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions components/HTMLBlock/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ const HTMLBlock = ({ children = '', html: htmlProp, runScripts, safeMode: safeMo
let html: string = '';
if (htmlProp !== undefined) {
html = htmlProp;
} else {
Comment thread
maximilianfalco marked this conversation as resolved.
if (typeof children !== 'string') {
throw new TypeError('HTMLBlock: children must be a string');
}
} else if (typeof children === 'string') {
html = children;
}

Expand Down
54 changes: 54 additions & 0 deletions lib/mdast-util/html-block-component/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import type { Html } from 'mdast';
import type { CompileContext, Extension as FromMarkdownExtension, Handle, Token } from 'mdast-util-from-markdown';

const contextMap = new WeakMap<Token, { chunks: string[]; lastEndLine: number }>();

function findHtmlBlockComponentToken(this: CompileContext): Token | undefined {
const events = this.tokenStack;
for (let i = events.length - 1; i >= 0; i -= 1) {
if (events[i][0].type === 'htmlBlockComponent') return events[i][0];
}
return undefined;
}

// Produces an MDAST `html` node, mdxishHtmlBlocks then transforms it into an `html-block` node
function enterHtmlBlockComponent(this: CompileContext, token: Parameters<Handle>[0]): void {
contextMap.set(token, { chunks: [], lastEndLine: token.start.line });
this.enter({ type: 'html', value: '' } as Html, token);
}

function exitHtmlBlockComponentData(this: CompileContext, token: Parameters<Handle>[0]): void {
const componentToken = findHtmlBlockComponentToken.call(this);
if (!componentToken) return;
const ctx = contextMap.get(componentToken);
if (ctx) {
const gap = token.start.line - ctx.lastEndLine;
if (ctx.chunks.length > 0 && gap > 0) {
ctx.chunks.push('\n'.repeat(gap));
}
ctx.chunks.push(this.sliceSerialize(token));
ctx.lastEndLine = token.end.line;
}
}

function exitHtmlBlockComponent(this: CompileContext, token: Parameters<Handle>[0]): void {
const ctx = contextMap.get(token);
const node = this.stack[this.stack.length - 1] as Html;
if (ctx) {
node.value = ctx.chunks.join('');
contextMap.delete(token);
}
this.exit(token);
}

export function htmlBlockComponentFromMarkdown(): FromMarkdownExtension {
return {
enter: {
htmlBlockComponent: enterHtmlBlockComponent,
},
exit: {
htmlBlockComponentData: exitHtmlBlockComponentData,
htmlBlockComponent: exitHtmlBlockComponent,
},
};
}
5 changes: 4 additions & 1 deletion lib/mdxish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ import tailwindTransformer from '../processor/transform/tailwind';

import { emptyTaskListItemFromMarkdown } from './mdast-util/empty-task-list-item';
import { gemojiFromMarkdown } from './mdast-util/gemoji';
import { htmlBlockComponentFromMarkdown } from './mdast-util/html-block-component';
import { jsxTableFromMarkdown } from './mdast-util/jsx-table';
import { legacyVariableFromMarkdown } from './mdast-util/legacy-variable';
import { magicBlockFromMarkdown } from './mdast-util/magic-block';
import { mdxComponentFromMarkdown } from './mdast-util/mdx-component';
import { gemoji } from './micromark/gemoji';
import { htmlBlockComponent } from './micromark/html-block-component';
import { jsxComment } from './micromark/jsx-comment';
import { jsxTable } from './micromark/jsx-table';
import { legacyVariable } from './micromark/legacy-variable';
Expand Down Expand Up @@ -158,8 +160,9 @@ export function mdxishAstProcessor(mdContent: string, opts: MdxishOpts = {}) {
text: mdxExprExt.text,
};

const micromarkExts = [jsxTable(), magicBlock(), mdxComponent(), gemoji(), legacyVariable(), looseHtmlEntity()];
const micromarkExts = [htmlBlockComponent(), jsxTable(), magicBlock(), mdxComponent(), gemoji(), legacyVariable(), looseHtmlEntity()];
Comment thread
maximilianfalco marked this conversation as resolved.
const fromMarkdownExts = [
htmlBlockComponentFromMarkdown(),
jsxTableFromMarkdown(),
magicBlockFromMarkdown(),
mdxComponentFromMarkdown(),
Expand Down
1 change: 1 addition & 0 deletions lib/micromark/html-block-component/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { htmlBlockComponent } from './syntax';
Loading
Loading