Skip to content

Commit

Permalink
test: update example and tests
Browse files Browse the repository at this point in the history
Integrate markdown-it-attrs plugin into the Eleventy configuration to allow setting attributes in Markdown. Updated test files to mock and verify expected rendering with the new plugin, ensuring functionality adheres to the added attribute features.
  • Loading branch information
kudoh committed Sep 28, 2024
1 parent 8307f55 commit 24baedd
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ The clipboard button is provided by [Iconify](https://iconify.design/) [content-
`eleventy shortcode function`

This shortcode initializes clipboard.js on window.onload event.
If clipboard copy succeeded, it shows a tooltips with message(default to `Copied!`).
If clipboard copy succeeded, it shows tooltips with message(default to `Copied!`).
Tooltips use [Primer Tooltips](https://primer.style/css/components/tooltips) CSS framework.

## Usage
Expand Down
4 changes: 3 additions & 1 deletion example/.eleventy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const syntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight');
const codeClipboard = require('eleventy-plugin-code-clipboard');
const markdownIt = require('markdown-it');
const markdownItAttrs = require('markdown-it-attrs');

module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(syntaxHighlight);
Expand All @@ -12,7 +13,8 @@ module.exports = function (eleventyConfig) {
const markdownLibrary = markdownIt({
html: true,
breaks: true,
}).use(codeClipboard.markdownItCopyButton);
}).use(markdownItAttrs)
.use(codeClipboard.markdownItCopyButton);

eleventyConfig.setLibrary('md', markdownLibrary);

Expand Down
24 changes: 22 additions & 2 deletions example/package-lock.json

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

3 changes: 2 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"devDependencies": {
"@11ty/eleventy": "^2.0.1",
"@11ty/eleventy-plugin-syntaxhighlight": "^3.1.3",
"eleventy-plugin-code-clipboard": "file:../"
"eleventy-plugin-code-clipboard": "file:../",
"markdown-it-attrs": "^4.2.0"
}
}
4 changes: 2 additions & 2 deletions example/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ layout: base.njk
echo "show clipboard button on code block"
```

```typescript
```typescript {.copy data-container=true}
console.log("show clipboard button");
```

## code block without language

```
Clipboard button is not displayed
```
```
4 changes: 2 additions & 2 deletions test/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ exports[`clipboard.js initialization default plugin config 1`] = `
exports[`custom renderer custom renderer config 1`] = `
"
<div style=\\"position: relative\\">
<div style=\\"position: relative\\" id=\\"code-container-0\\" class=\\"test\\">
<pre class=\\"language-yaml\\"><code id=\\"code-0\\">test: unit-test<br />type: 11ty</code></pre>
<button class=\\"test-button another-button-class\\"
data-clipboard-target=\\"#code-0\\"
Expand All @@ -25,7 +25,7 @@ exports[`custom renderer custom renderer config 1`] = `
exports[`custom renderer default renderer config 1`] = `
"
<div style=\\"position: relative\\">
<div style=\\"position: relative\\" id=\\"code-container-0\\" class=\\"test\\">
<pre class=\\"language-yaml\\"><code id=\\"code-0\\">test: unit-test<br />type: 11ty</code></pre>
<button class=\\"code-copy \\"
data-clipboard-target=\\"#code-0\\"
Expand Down
21 changes: 19 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('clipboard.js initialization', () => {

describe('custom renderer', () => {
let md;
const mockRenderAttrs = jest.fn().mockReturnValue('class="test"');

beforeEach(() => {
md = {
Expand All @@ -41,11 +42,21 @@ describe('custom renderer', () => {
};
});

afterEach(() => {
mockRenderAttrs.mockClear();
});

test('default renderer config', () => {
plugin.configFunction(eleventyConfig);
plugin.markdownItCopyButton(md);
const token = { tag: 'code', info: 'yaml', content: 'test: unit-test\ntype: 11ty' };
const html = md.renderer.rules.fence([token], 0);
const html = md.renderer.rules.fence(
[token],
0,
undefined,
undefined,
{ renderAttrs: mockRenderAttrs },
);
expect(html).toMatchSnapshot();
});

Expand All @@ -65,7 +76,13 @@ describe('custom renderer', () => {
};
plugin.markdownItCopyButton(md, customRendererConfig);
const token = { tag: 'code', info: 'yaml', content: 'test: unit-test\ntype: 11ty' };
const html = md.renderer.rules.fence([token], 0);
const html = md.renderer.rules.fence(
[token],
0,
undefined,
undefined,
{ renderAttrs: mockRenderAttrs },
);
expect(html).toMatchSnapshot();
});

Expand Down

0 comments on commit 24baedd

Please sign in to comment.