Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
build(aio): convert triple backtick blocks to code-example tags
Closes #16246
  • Loading branch information
petebacondarwin committed Apr 29, 2017
commit 3cff74117944f0885f1953b3f1f23026a06cd840
11 changes: 11 additions & 0 deletions aio/content/marketing/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ <h2>&lt;code-example&gt;</h2>
&lt;/hero-details&gt;
</code-example>

## Backticked code blocks

```html
<hero-details>
<h2>Mister Fantastic</h2>
<hero-team>
<h3>Losing Team</h3>
</hero-team>
</hero-details>
```

<h2>&lt;live-example&gt;</h2>

<p>Plain live-example</p>
Expand Down
14 changes: 14 additions & 0 deletions aio/tools/transforms/remark-package/services/handlers/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Render markdown code blocks as `<code-example>` 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 }]);
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const remark = require('remark');
const html = require('remark-html');
const code = require('./handlers/code');

/**
* @dgService renderMarkdown
* @description
* Render the markdown in the given string as HTML.
*/
module.exports = function renderMarkdown() {
const handlers = { code };
const renderer = remark()
.use(inlineTagDefs)
.use(noIndentedCodeBlocks)
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,21 @@ describe('remark: renderMarkdown service', () => {
const output = renderMarkdown(content);
expect(output).toEqual('<p>some text</p>\n<p> indented text</p>\n<p>other text</p>\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(
'<code-example language="ts">\n' +
' class MyClass {\n' +
' method1() { ... }\n' +
' }\n' +
'</code-example>\n'
);
});
});