-
Notifications
You must be signed in to change notification settings - Fork 129
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 Copy Button to Code Blocks #138
Changes from all commits
fffea9d
bd8a077
22afe87
93ef7c3
73f3afa
8039dd4
2d1f7a0
41800cb
d39d863
63b34c8
55c4e00
e6c0903
8e356ae
c8b1d1f
19b3463
5e05e7f
b7e027e
340c2e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* hide the langauge label provided by MarkdownIt */ | ||
|
||
pre code::after { | ||
display: none; | ||
} | ||
|
||
/* copied from default.css with minimal changes positioning & color */ | ||
|
||
pre { | ||
position: relative; | ||
} | ||
.m-mdic-copy-wrapper { | ||
position: absolute; | ||
top: 4px; | ||
right: 8px; | ||
} | ||
.m-mdic-copy-wrapper span.u-mdic-copy-code_lang { | ||
position: absolute; | ||
top: 3px; | ||
right: calc(100% + 4px); | ||
font-family: system-ui; | ||
font-size: 12px; | ||
line-height: 18px; | ||
color: #666; | ||
opacity: 1; | ||
} | ||
.m-mdic-copy-wrapper div.u-mdic-copy-notify { | ||
display: none; | ||
position: absolute; | ||
top: 0; | ||
right: 0; | ||
padding: 3px 6px; | ||
border: 0; | ||
border-radius: 3px; | ||
background: none; | ||
font-family: system-ui; | ||
font-size: 12px; | ||
line-height: 18px; | ||
color: #666; | ||
opacity: 0.3; | ||
outline: none; | ||
opacity: 1; | ||
right: 100%; | ||
padding-right: 35px; | ||
} | ||
.m-mdic-copy-wrapper button.u-mdic-copy-btn { | ||
position: relative; | ||
top: 0; | ||
right: 0; | ||
padding: 3px 6px; | ||
border: 0; | ||
border-radius: 3px; | ||
background: none; | ||
font-family: system-ui; | ||
font-size: 12px; | ||
line-height: 18px; | ||
color: #666; | ||
opacity: 1; | ||
outline: none; | ||
cursor: pointer; | ||
transition: opacity 0.2s; | ||
} | ||
.m-mdic-copy-wrapper button.u-mdic-copy-btn:hover { | ||
opacity: 1; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ describe("parseMarkdown(input)", () => { | |
const snapshot = parseMarkdown(await readFile(path, "utf8"), "test/input", name); | ||
let allequal = true; | ||
for (const ext of ["html", "json"]) { | ||
const actual = ext === "json" ? jsonMeta(snapshot) : snapshot[ext]; | ||
const actual = applySpecialCaseFilters(ext === "json" ? jsonMeta(snapshot) : snapshot[ext]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll want the build to be always reproducible, not only in tests; otherwise it's going to be painful for people who track changes of the artifacts, or when you who use a deploy script based on file differences. |
||
const outfile = resolve(outputRoot, `${basename(outname, ".md")}.${ext}`); | ||
const diffile = resolve(outputRoot, `${basename(outname, ".md")}-changed.${ext}`); | ||
let expected; | ||
|
@@ -70,3 +70,14 @@ function jsonMeta({html, ...rest}: ParseResult): string { | |
function jsonEqual(a: string, b: string): boolean { | ||
return deepEqual(JSON.parse(a), JSON.parse(b)); | ||
} | ||
|
||
function applySpecialCaseFilters(snapshotContent: string): string { | ||
// if the snapshot contains markdown-it-copy code, strip out j-notify id, | ||
// which is regenerated on every run and thus always different | ||
|
||
if (snapshotContent.includes("mdic")) { | ||
return snapshotContent.replace(/(id=\\?\\?"j-notify)([\d-]*)/g, "$1"); | ||
} | ||
|
||
return snapshotContent; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,8 @@ <h1 id="fenced-code" tabindex="-1"><a class="observablehq-header-anchor" href="# | |
<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">add</span>(<span class="hljs-params">a, b</span>) { | ||
<span class="hljs-keyword">return</span> a + b; | ||
} | ||
</code></pre> | ||
</code><div class="m-mdic-copy-wrapper"><span class="u-mdic-copy-code_lang">js</span><div class="u-mdic-copy-notify" id="j-notify">success</div><button class="u-mdic-copy-btn j-mdic-copy-btn" data-mdic-content="function add(a, b) { | ||
return a + b; | ||
} | ||
" data-mdic-attach-content="" data-mdic-notify-id="j-notify" data-mdic-notify-delay="2000" data-mdic-copy-fail-text="fail" onclick="!function(t){const e={copy:(t='',e='')=>new Promise((c,o)=>{const n=document.createElement('textarea'),d=e?`\n\n${e}`:e;n.value=`${t}${d}`,document.body.appendChild(n),n.select();try{const t=document.execCommand('copy');document.body.removeChild(n),t?c():o()}catch(t){document.body.removeChild(n),o()}}),btnClick(t){const c=t&&t.dataset?t.dataset:{},o=c.mdicNotifyId,n=document.getElementById(o),d=c.mdicNotifyDelay,i=c.mdicCopyFailText;e.copy(c.mdicContent,c.mdicAttachContent).then(()=>{n.style.display='block',setTimeout(()=>{n.style.display='none'},d)}).catch(()=>{alert(i)})}};e.btnClick(t)}(this);">copy</button></div></pre> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should roll our own, without this obfuscated code. |
||
</div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.