|
| 1 | +# <code-block> |
| 2 | + |
| 3 | +[](LICENSE) [](https://www.npmjs.com/package/@heppokofrontend/html-code-block-element) [](https://www.webcomponents.org/element/@heppokofrontend/html-code-block-element) [](https://www.jsdelivr.com/package/npm/@heppokofrontend/html-code-block-element) [](https://codeclimate.com/github/heppokofrontend/html-code-block-element/maintainability) [](https://codeclimate.com/github/heppokofrontend/html-code-block-element/test_coverage) [](https://snyk.io/package/npm/@heppokofrontend/html-code-block-element) |
| 4 | + [](https://snyk.io/advisor/npm-package/@heppokofrontend/html-code-block-element) |
| 5 | + |
| 6 | +Code block custom element with syntax highlighting and copy button. |
| 7 | + |
| 8 | +It has [highlight.js](https://www.npmjs.com/package/highlight.js?activeTab=readme) for syntax highlighting. |
| 9 | + |
| 10 | +## Usage |
| 11 | + |
| 12 | +### In browser |
| 13 | + |
| 14 | +It can be used by loading html-code-block-element.common.min.js and one CSS theme. |
| 15 | + |
| 16 | +The highlight.js style is available on CDN. You can also download the JS and CSS from the respective repositories and load them into your page. |
| 17 | + |
| 18 | +```html |
| 19 | +<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.1.0/styles/vs.min.css" /> |
| 20 | +<script src="https://cdn.jsdelivr.net/npm/@heppokofrontend/html-code-block-element@1.0.0/dist/html-code-block-element.common.min.js" defer></script> |
| 21 | +``` |
| 22 | + |
| 23 | +There are three versions of this library available. |
| 24 | + |
| 25 | +- `html-code-block-element.common.min.js` - One that supports only the popular languages. |
| 26 | +- `html-code-block-element.all.min.js` - One that enables [all languages](https://github.com/highlightjs/highlight.js/blob/main/SUPPORTED_LANGUAGES.md) supported by highligh.js. |
| 27 | +- `html-code-block-element.core.min.js` - For developers who want to do their own `hljs.registerLanguage()`. |
| 28 | + |
| 29 | + |
| 30 | +#### Example |
| 31 | + |
| 32 | +**Note:** The textarea element cannot be included in the content of the textarea element. If you want to include it, please use HTML Entity for the tag. |
| 33 | + |
| 34 | +```html |
| 35 | +<code-block language="html" label="example.html" controls> |
| 36 | +<textarea><script>console.log(true);</script></textarea> |
| 37 | +</code-block> |
| 38 | +``` |
| 39 | + |
| 40 | +or |
| 41 | + |
| 42 | +```html |
| 43 | +<code-block language="html" label="example.html" controls> |
| 44 | +<script>console.log(true);</script> |
| 45 | +</code-block> |
| 46 | +``` |
| 47 | + |
| 48 | +#### Spec |
| 49 | + |
| 50 | +- **Categories:** |
| 51 | + - [Flow content](https://html.spec.whatwg.org/multipage/dom.html#flow-content-2). |
| 52 | + - [Palpable content](https://html.spec.whatwg.org/multipage/dom.html#palpable-content-2). |
| 53 | +- **Contexts in which this element can be used:** |
| 54 | + - Where flow content is expected. |
| 55 | +- **Content model:** |
| 56 | + - [Text](https://html.spec.whatwg.org/multipage/dom.html#text-content) or one [textarea](https://html.spec.whatwg.org/multipage/form-elements.html#the-textarea-element) element |
| 57 | +- **Tag omission in text/html:** |
| 58 | + - Neither tag is omissible. |
| 59 | +- **Content attributes:** |
| 60 | + - [Global attributes](https://html.spec.whatwg.org/multipage/dom.html#global-attributes) |
| 61 | + - `controls` - Show controls |
| 62 | + - `label` - Give the code block a name. If omitted, the name will be "code block". |
| 63 | + - `language` - Language name of the code. If omitted, it will be detected automatically. |
| 64 | +- **Accessibility considerations:** |
| 65 | + - [No corresponding role](https://w3c.github.io/html-aria/#dfn-no-corresponding-role) |
| 66 | + - `role` attribute is not allowed |
| 67 | + - `aria-*` attribute is not allowed |
| 68 | + |
| 69 | +#### DOM interface |
| 70 | + |
| 71 | +```java |
| 72 | +[Exposed=Window] |
| 73 | +interface HTMLCodeBlockElement : HTMLElement { |
| 74 | + [HTMLConstructor] constructor(); |
| 75 | + |
| 76 | + [CEReactions] attribute boolean controls; |
| 77 | + [CEReactions] attribute DOMString label; |
| 78 | + [CEReactions] attribute DOMString language; |
| 79 | + [CEReactions] attribute DOMString value; |
| 80 | +}; |
| 81 | +``` |
| 82 | + |
| 83 | +### In development |
| 84 | + |
| 85 | +#### Installation: |
| 86 | + |
| 87 | +```shell |
| 88 | +npm install --save @heppokofrontend/html-code-block-element |
| 89 | +``` |
| 90 | + |
| 91 | +#### Usage: |
| 92 | + |
| 93 | +The `customElements.define()` will also be included. |
| 94 | + |
| 95 | +```javascript |
| 96 | +// For highlighting, `highlight.js/lib/common` will be used. |
| 97 | +import 'html-code-block-element'; |
| 98 | +// For highlighting, `highlight.js` will be used. |
| 99 | +import 'html-code-block-element/dist/index.all'; |
| 100 | +// For highlighting, `highlight.js/lib/code` will be used. |
| 101 | +import 'html-code-block-element/dist/index.core'; |
| 102 | +``` |
| 103 | + |
| 104 | +If you are using purely constructors: |
| 105 | + |
| 106 | +```javascript |
| 107 | +import HTMLCodeBlockElement from 'html-code-block-element/dist/class/HTMLCodeBlockElement'; |
| 108 | +``` |
| 109 | + |
| 110 | +#### Use as constructor |
| 111 | + |
| 112 | +Manual setup: |
| 113 | + |
| 114 | +```js |
| 115 | +import hljs from 'highlight.js/lib/core'; |
| 116 | +import javascript from 'highlight.js/lib/languages/javascript'; |
| 117 | +import HTMLCodeBlockElement from 'html-code-block-element/dist/class/HTMLCodeBlockElement'; |
| 118 | +// or import { HTMLCodeBlockElement } from 'html-code-block-element'; |
| 119 | + |
| 120 | +// Support JavaScript |
| 121 | +hljs.registerLanguage('javascript', javascript); |
| 122 | + |
| 123 | +// Set endgine |
| 124 | +HTMLCodeBlockElement.endgine = hljs; |
| 125 | + |
| 126 | +// Define |
| 127 | +customElements.define('code-block', HTMLCodeBlockElement); |
| 128 | + |
| 129 | +// Use |
| 130 | +const cbElm = new HTMLCodeBlockElement(); |
| 131 | +``` |
| 132 | + |
| 133 | +##### Syntax |
| 134 | + |
| 135 | +No params. |
| 136 | + |
| 137 | +```js |
| 138 | +new HTMLCodeBlockElement(); |
| 139 | +``` |
| 140 | + |
| 141 | +## Support browser |
| 142 | + |
| 143 | +- Chrome |
| 144 | +- Safari |
| 145 | +- Firefox |
| 146 | +- Edge |
0 commit comments