-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3380725
commit 8062b7c
Showing
4 changed files
with
79 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// @unocss-include | ||
import Clipboard from 'clipboard' | ||
|
||
const PLUGIN_CLASS = 'code-copy' | ||
|
||
new Clipboard(`.${PLUGIN_CLASS}`, { | ||
text: (trigger: HTMLButtonElement) => { | ||
trigger.classList.add('i-mdi-checkbox-multiple-marked-outline') | ||
setTimeout(() => { | ||
trigger.classList.remove('i-mdi-checkbox-multiple-marked-outline') | ||
}, 2000) | ||
return trigger.getAttribute('data-clipboard-text') ?? '' | ||
} | ||
}) | ||
|
||
interface Options { | ||
buttonClass?: string | ||
} | ||
|
||
type RulesArgs = [Array<{ content: string }>, number] | ||
|
||
const defaultOptions: Options = { | ||
buttonClass: | ||
'text-white h-5 absolute cursor-pointer i-mdi-checkbox-multiple-blank-outline top-6 right-2' | ||
} | ||
|
||
const renderCode = ( | ||
origRule: (...args: RulesArgs) => string, | ||
options: Options | ||
) => { | ||
options = { ...defaultOptions, ...options } | ||
return (...args: RulesArgs) => { | ||
const [tokens, idx] = args | ||
const content = tokens[idx].content | ||
.replaceAll('"', '"') | ||
.replaceAll("'", '<') | ||
const origRendered = origRule(...args) | ||
|
||
if (content.length === 0) { | ||
return origRendered | ||
} | ||
|
||
return ` | ||
<div style="position: relative"> | ||
${origRendered} | ||
<button class="${PLUGIN_CLASS} ${ | ||
options.buttonClass ?? '' | ||
}" data-clipboard-text="${content}" title="Copy code"> | ||
</button> | ||
</div> | ||
` | ||
} | ||
} | ||
|
||
export default (md: any, options: Options) => { | ||
md.renderer.rules.code_block = renderCode( | ||
md.renderer.rules.code_block, | ||
options | ||
) | ||
md.renderer.rules.fence = renderCode(md.renderer.rules.fence, options) | ||
} |