diff --git a/aio/content/marketing/test.html b/aio/content/marketing/test.html index e63e24cae1..74e5c435a7 100644 --- a/aio/content/marketing/test.html +++ b/aio/content/marketing/test.html @@ -85,6 +85,17 @@ </hero-details> +## Backticked code blocks + +```html + +

Mister Fantastic

+ +

Losing Team

+
+
+``` +

<live-example>

Plain live-example

diff --git a/aio/tools/transforms/remark-package/services/handlers/code.js b/aio/tools/transforms/remark-package/services/handlers/code.js new file mode 100644 index 0000000000..a208704e93 --- /dev/null +++ b/aio/tools/transforms/remark-package/services/handlers/code.js @@ -0,0 +1,14 @@ +/** + * Render markdown code blocks as `` tags + */ +module.exports = function code(h, node) { + var value = node.value ? ('\n' + node.value + '\n') : ''; + var lang = node.lang && node.lang.match(/^[^ \t]+(?=[ \t]|$)/); + var props = {}; + + if (lang) { + props.language = lang; + } + + return h(node, 'code-example', props, [{ type: 'text', value }]); +}; diff --git a/aio/tools/transforms/remark-package/services/renderMarkdown.js b/aio/tools/transforms/remark-package/services/renderMarkdown.js index 5791bd5f90..bb0defe5f2 100644 --- a/aio/tools/transforms/remark-package/services/renderMarkdown.js +++ b/aio/tools/transforms/remark-package/services/renderMarkdown.js @@ -1,5 +1,6 @@ const remark = require('remark'); const html = require('remark-html'); +const code = require('./handlers/code'); /** * @dgService renderMarkdown @@ -7,6 +8,7 @@ const html = require('remark-html'); * Render the markdown in the given string as HTML. */ module.exports = function renderMarkdown() { + const handlers = { code }; const renderer = remark() .use(inlineTagDefs) .use(noIndentedCodeBlocks) @@ -15,7 +17,7 @@ module.exports = function renderMarkdown() { // .use(() => tree => { // console.log(require('util').inspect(tree, { colors: true, depth: 4 })); // }) - .use(html); + .use(html, { handlers }); return function renderMarkdownImpl(content) { return renderer.processSync(content).toString(); diff --git a/aio/tools/transforms/remark-package/services/renderMarkdown.spec.js b/aio/tools/transforms/remark-package/services/renderMarkdown.spec.js index 989512bb57..ea4409ac5f 100644 --- a/aio/tools/transforms/remark-package/services/renderMarkdown.spec.js +++ b/aio/tools/transforms/remark-package/services/renderMarkdown.spec.js @@ -67,4 +67,21 @@ describe('remark: renderMarkdown service', () => { const output = renderMarkdown(content); expect(output).toEqual('

some text

\n

indented text

\n

other text

\n'); }); + + it('should format triple backtick code blocks as `code-example` tags', () => { + const content = + '```ts\n' + + ' class MyClass {\n' + + ' method1() { ... }\n' + + ' }\n' + + '```'; + const output = renderMarkdown(content); + expect(output).toEqual( + '\n' + + ' class MyClass {\n' + + ' method1() { ... }\n' + + ' }\n' + + '\n' + ); + }); });