|
1 | 1 | # codeplus |
| 2 | + |
| 3 | +Add interactive elements to your `<code>` without a bunch of custom markup or JavaScript. |
| 4 | + |
| 5 | +- filenames |
| 6 | +- tabs |
| 7 | +- copy button |
| 8 | + |
| 9 | +The key to codeplus is that you don't have to do much to make it work, |
| 10 | +and your code blocks also look fine when it's not in use. |
| 11 | +If you're writing docs in markdown, |
| 12 | +for example, |
| 13 | +you want those to look *normal* on GitHub and other sites where you don't have control over the markdown rendering. |
| 14 | +Users should still be able to read everything in that form. |
| 15 | +But when you render the same code on *your* site, |
| 16 | +you get an extra layer of interactivity just by adding `codeplus` on top. |
| 17 | + |
| 18 | +This library does not do syntax highlighting! |
| 19 | +That means you can use any server-side (or client-side) syntax highlighter you want and codeplus will add the interactive features after the fact. |
| 20 | + |
| 21 | +To render filenames all you need to do is start your code block with a comment line that starts with `# <filename>:` |
| 22 | + |
| 23 | +```yaml |
| 24 | +# .pullapprove.yml |
| 25 | +version: 3 |
| 26 | +groups: ... |
| 27 | +``` |
| 28 | +
|
| 29 | +You can also change the display name by using parentheses after the filename: |
| 30 | +
|
| 31 | +```yaml |
| 32 | +# .pullapprove.yml (GitHub) |
| 33 | +version: 3 |
| 34 | +groups: ... |
| 35 | +``` |
| 36 | +
|
| 37 | +To get a group of tabs, just use the same pattern followed by a `- <group identifider>`: |
| 38 | + |
| 39 | +```yaml |
| 40 | +# .pullapprove.yml (GitHub) - Example group |
| 41 | +version: 3 |
| 42 | +groups: ... |
| 43 | +``` |
| 44 | + |
| 45 | +```yaml |
| 46 | +# .pullapprove.yml (Bitbucket) - Example group |
| 47 | +version: 3 |
| 48 | +groups: ... |
| 49 | +``` |
| 50 | + |
| 51 | +## Installation |
| 52 | + |
| 53 | +```sh |
| 54 | +npm install @dropseed/codeplus |
| 55 | +``` |
| 56 | + |
| 57 | +```js |
| 58 | +import Codeplus from "@dropseed/codeplus"; |
| 59 | +
|
| 60 | +window.addEventListener('load', function() { |
| 61 | + new Codeplus({}).render(); |
| 62 | +}); |
| 63 | +``` |
| 64 | + |
| 65 | +### CDN |
| 66 | + |
| 67 | +```html |
| 68 | +<script src="https://unpkg.com/@dropseed/codeplus@0.2.0/dist/browser.js"></script> |
| 69 | +<script> |
| 70 | +window.addEventListener("load", function() { |
| 71 | + new Codeplus({}).render(); |
| 72 | +}); |
| 73 | +``` |
| 74 | + |
| 75 | +## Options |
| 76 | + |
| 77 | +```js |
| 78 | +new Codeplus({ |
| 79 | + // The query selector to find your code elements |
| 80 | + selector: "pre > code", |
| 81 | + // Saves a user's tab selection in localStorage and shows that tab on page load (ex. "Python") |
| 82 | + rememberTabSelections: true, |
| 83 | + // Enable debug console.logs |
| 84 | + debug: false, |
| 85 | + // Use classes to add styling (or look at the default CSS classes) |
| 86 | + instanceClass: "rounded-t-none group", |
| 87 | + navClass: "bg-black rounded-t overflow-hidden", |
| 88 | + tabClass: "px-3 py-2 text-sm text-gray-300 hover:text-gray-100", |
| 89 | + activeTabClass: "bg-[#282c34]", |
| 90 | + inactiveTabClass: "", |
| 91 | + copyButtonClass: "items-center group-hover:flex hidden rounded border border-gray-200 px-2 py-1 text-sm", |
| 92 | + // Custom render for the tabs |
| 93 | + renderTab: function(tab, instance) { |
| 94 | + const icon = "..."; |
| 95 | + tab.innerHTML = icon + tab.innerHTML; |
| 96 | + }, |
| 97 | + // Custom render for the copy button |
| 98 | + renderCopyButton: function(copyButton, instance, copied) { |
| 99 | + if (!copied) { |
| 100 | + copyButton.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" class="w-4 h-4 bi bi-clipboard" viewBox="0 0 16 16"><path d="M4 1.5H3a2 2 0 0 0-2 2V14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V3.5a2 2 0 0 0-2-2h-1v1h1a1 1 0 0 1 1 1V14a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3.5a1 1 0 0 1 1-1h1v-1z"/><path d="M9.5 1a.5.5 0 0 1 .5.5v1a.5.5 0 0 1-.5.5h-3a.5.5 0 0 1-.5-.5v-1a.5.5 0 0 1 .5-.5h3zm-3-1A1.5 1.5 0 0 0 5 1.5v1A1.5 1.5 0 0 0 6.5 4h3A1.5 1.5 0 0 0 11 2.5v-1A1.5 1.5 0 0 0 9.5 0h-3z"/></svg> Copy`; |
| 101 | + } |
| 102 | + }, |
| 103 | +}).render(); |
| 104 | +``` |
| 105 | + |
| 106 | +## Styling |
| 107 | + |
| 108 | +Styling can be done either with inserting classes via [options](#options) or by using the default CSS classes: |
| 109 | + |
| 110 | +```css |
| 111 | +.codeplus {} |
| 112 | +.codeplus-group {} |
| 113 | +.codeplus-nav {} |
| 114 | +.codeplus-tab {} |
| 115 | +.codeplus-tab.active {} |
| 116 | +.codeplus-tab.inactive {} |
| 117 | +.codeplus-instances {} |
| 118 | +.codeplus-instance {} |
| 119 | +.codeplus-copy-btn {} |
| 120 | +``` |
| 121 | + |
| 122 | +By default there are only a few styles for basic layout help. |
| 123 | +Any colors, font sizes, etc. are up to you and will probably be similar to your syntax highlighting theme. |
0 commit comments