Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code copy buttons using 'markdown-it-copy' plugin #203

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
30 changes: 30 additions & 0 deletions src/parser/copycode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import MarkdownIt from 'markdown-it';
import octicons from '@primer/octicons';

const copyIcon = octicons['copy'].toSVG({ class: 'icon-copy' });
const checkIcon = octicons['check'].toSVG({ class: 'icon-check' });
const xIcon = octicons['x'].toSVG({ class: 'icon-x' });

export default function copycode(md: MarkdownIt) {
const defaultRender = md.renderer.rules.fence!;
md.renderer.rules.fence = (tokens, idx, options, env, self) => {
const renderedPreBlock = defaultRender(tokens, idx, options, env, self);
const content = tokens[idx].content;
return `
<div class="pre-wrapper" style="position: relative">
${renderedPreBlock}
<div class="copy-wrapper">
<button class="copy-button" data-clipboard-text="${content.replaceAll('"', '&quot;')}">
${copyIcon}
</button>
<div class="copy-success">
${checkIcon}
</div>
<div class="copy-fail">
${xIcon}
</div>
</div>
</div>
`;
};
}
2 changes: 2 additions & 0 deletions src/parser/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import MarkdownIt from 'markdown-it';
import anchor from 'markdown-it-anchor';
import copycode from './copycode.js';
import frontMatter from 'markdown-it-front-matter';
import highlight from './highlight.js';
import graphviz from './dot.js';
Expand Down Expand Up @@ -68,6 +69,7 @@ mdit.use(anchor, {
placement: 'before',
}),
});
mdit.use(copycode);
mdit.use(graphviz);
mdit.use(githubAlerts);
mdit.use(mermaid);
Expand Down
2 changes: 2 additions & 0 deletions src/routes/viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ router.get(/.*/, async (req: Request, res: Response) => {
</script>
${config.scripts ? `<script type="text/javascript">${config.scripts}</script>` : ''}
<script type="module" src="/static/client.mjs"></script>
<script type="text/javascript" src="/static/copycode/clipboard.min.js"></script>
<script type="text/javascript" src="/static/copycode/client.js"></script>
</html>
`);
});
Expand Down
14 changes: 14 additions & 0 deletions static/colors.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
--icon-file: var(--text-secondary);
--icon-back: var(--text-secondary);

--copy-button-fg: #aaa;
--copy-button-bg: #333;
--copy-success-fg: #ddffcc;
--copy-success-bg: #5c993d;
--copy-fail-fg: #ffcccc;
--copy-fail-bg: #993d3d;

--syntax-text: var(--text-primary);
--syntax-keyword: #aed7ff;
--syntax-entity: #aeafff;
Expand Down Expand Up @@ -69,6 +76,13 @@
--icon-file: #636c76;
--icon-back: var(--text-secondary);

--copy-button-fg: #1f2328;
--copy-button-bg: #e7e9eb;
--copy-success-fg: #154000;
--copy-success-bg: #c7e6b8;
--copy-fail-fg: #400000;
--copy-fail-bg: #e6b8b8;

/* source for light mode syntax theme:
* https://github.com/highlightjs/highlight.js/blob/main/src/styles/github.css
* */
Expand Down
26 changes: 26 additions & 0 deletions static/copycode/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
document.querySelectorAll('.copy-success, .copy-fail').forEach((element) => {
element.style.display = 'none';
});

const Notify = {
Success: '.copy-success',
Fail: '.copy-fail',
};

let clipboard = new ClipboardJS('.copy-button');

clipboard.on('success', function (e) {
showNotification(e.trigger, Notify.Success);
});

clipboard.on('error', function (e) {
showNotification(e.trigger, Notify.Fail);
});

function showNotification(btn, notify) {
const notificationElement = btn.parentElement.querySelector(notify);
notificationElement.style.display = 'flex';
setTimeout(() => {
notificationElement.style.display = 'none';
}, 2000);
}
7 changes: 7 additions & 0 deletions static/copycode/clipboard.min.js

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

44 changes: 44 additions & 0 deletions static/markdown.css
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,50 @@ blockquote {
color: var(--alert-caution);
}

/* --------------------------------------------------------------------------
* COPY-CODE-BUTTON --------------------------------------------------------- */

.copy-wrapper {
position: absolute;
top: 0.5rem;
right: 0.5rem;
}
.copy-button, .copy-success, .copy-fail {
position: absolute;
top: 0;
right: 0;
display: flex;
align-items: center;
justify-content: center;
height: 2rem;
width: 2rem;
padding: 0.4rem;
border: 0;
border-radius: 0.4rem;
background: none;
fill: var(--copy-button-fg);
cursor: pointer;
transition: opacity 0.2s, background 0.2s;
}
.copy-button {
opacity: 0;
}
pre:hover + .copy-wrapper > .copy-button {
opacity: 0.3;
}
.copy-button:hover {
opacity: 1;
background-color: var(--copy-button-bg);
}
.copy-success {
background: var(--copy-success-bg);
fill: var(--copy-success-fg);
}
.copy-fail {
background: var(--copy-fail-bg);
fill: var(--copy-fail-fg);
}

/* --------------------------------------------------------------------------
* MISCELLANEOUS ------------------------------------------------------------ */

Expand Down