11# < ; code-block> ;
22
33[](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/test/npm/@heppokofrontend/html-code-block-element)
4- [ ![ @heppokofrontend/html-code-block-element ] ( https://snyk.io/advisor/npm-package/@heppokofrontend/html-code-block-element/badge.svg )] ( https://snyk.io/advisor/npm-package/@heppokofrontend/html-code-block-element )
4+ [ ![ @heppokofrontend/html-code-block-element ] ( https://snyk.io/advisor/npm-package/@heppokofrontend/html-code-block-element/badge.svg )] ( https://snyk.io/advisor/npm-package/@heppokofrontend/html-code-block-element )
55
66Code block custom element with syntax highlighting and copy button.
77
@@ -22,9 +22,10 @@ DEMO: <https://heppokofrontend.github.io/html-code-block-element/>
2222</custom-element-demo>
2323```
2424-->
25+
2526``` html
2627<code-block language =" html" label =" example.html" controls >
27- < ; script> ; console.log(true);< ; /script> ;
28+ < ; script> ; console.log(true);< ; /script> ;
2829</code-block >
2930```
3031
@@ -35,8 +36,14 @@ It can be used by loading html-code-block-element.common.min.js and one CSS them
3536The 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.
3637
3738``` html
38- <link rel =" stylesheet" href =" https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.1.0/styles/vs.min.css" />
39- <script src =" https://cdn.jsdelivr.net/npm/@heppokofrontend/html-code-block-element/lib/html-code-block-element.common.min.js" defer ></script >
39+ <link
40+ rel =" stylesheet"
41+ href =" https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.1.0/styles/vs.min.css"
42+ />
43+ <script
44+ src =" https://cdn.jsdelivr.net/npm/@heppokofrontend/html-code-block-element/lib/html-code-block-element.common.min.js"
45+ defer
46+ ></script >
4047```
4148
4249There are three versions of this library available.
@@ -45,22 +52,21 @@ There are three versions of this library available.
4552- ` 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.
4653- ` html-code-block-element.core.min.js ` - For developers who want to do their own ` hljs.registerLanguage() ` .
4754
48-
4955#### Example
5056
5157** 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.
5258
5359``` html
5460<code-block language =" html" label =" example.html" controls >
55- <textarea ><script >console .log (true ); </script ></textarea >
61+ <textarea ><script >console .log (true ); </script ></textarea >
5662</code-block >
5763```
5864
5965or
6066
6167``` html
6268<code-block language =" html" label =" example.html" controls >
63- < ; script> ; console.log(true);< ; /script> ;
69+ < ; script> ; console.log(true);< ; /script> ;
6470</code-block >
6571```
6672
7884- ** Content attributes:**
7985 - [ Global attributes] ( https://html.spec.whatwg.org/multipage/dom.html#global-attributes )
8086 - ` controls ` - Show controls
87+ - ` notrim ` - Does't remove whitespace from both ends of a source
8188 - ` label ` - Give the code block a unique name. If omitted, it will always have the accessible name "Code Block".
8289 - ` language ` - Language name of the code. If omitted, it will be detected automatically.
8390- ** Accessibility considerations:**
@@ -93,6 +100,7 @@ interface HTMLCodeBlockElement : HTMLElement {
93100 [HTMLConstructor ] constructor ();
94101
95102 [CEReactions ] attribute boolean controls;
103+ [CEReactions ] attribute boolean notrim;
96104 [CEReactions ] attribute DOMString label;
97105 [CEReactions ] attribute DOMString language;
98106 [CEReactions ] attribute DOMString value;
@@ -129,7 +137,7 @@ import HTMLCodeBlockElement from '@heppokofrontend/html-code-block-element/dist/
129137#### Use in React
130138
131139This package contains the global type files for React.
132-
140+
133141- ` React.CodeBlockHTMLAttributes `
134142- ` code-block ` in ` JSX.IntrinsicElements `
135143
@@ -139,14 +147,15 @@ This package contains the global type files for React.
139147// CodeBlock.tsx
140148import React , {useEffect , CodeBlockHTMLAttributes } from ' react' ;
141149
142- export const CodeBlock: React .FC <CodeBlockHTMLAttributes <HTMLElement >> = ({children , ... props }) => {
150+ export const CodeBlock: React .FC <CodeBlockHTMLAttributes <HTMLElement >> = ({
151+ children ,
152+ ... props
153+ }) => {
143154 useEffect (() => {
144155 import (` @heppokofrontend/html-code-block-element ` );
145156 });
146157
147- return (
148- <code-block { ... props } >{ children } </code-block >
149- );
158+ return <code-block { ... props } >{ children } </code-block >;
150159};
151160```
152161
@@ -164,7 +173,6 @@ import HTMLCodeBlockElement from '@heppokofrontend/html-code-block-element/dist/
164173// Support JavaScript
165174hljs .registerLanguage (' javascript' , javascript);
166175
167-
168176// 2. Set endgine
169177/**
170178 * Example: Callback to be called internally
@@ -178,10 +186,7 @@ HTMLCodeBlockElement.highlight = function (src, options) {
178186 options? .language &&
179187 hljs .getLanguage (options .language )
180188 ) {
181- const {value } = hljs .highlight (
182- src,
183- options,
184- );
189+ const {value } = hljs .highlight (src, options);
185190
186191 return {
187192 markup: value,
@@ -191,25 +196,21 @@ HTMLCodeBlockElement.highlight = function (src, options) {
191196 return {
192197 markup: hljs .highlightAuto (src).value ,
193198 };
194- }
195-
199+ };
196200
197201// 3. Define
198202customElements .define (' code-block' , HTMLCodeBlockElement);
199203
200-
201204// 4. Make
202205const cbElm = new HTMLCodeBlockElement ();
203206
204-
205207// 5. Assign
206208cbElm .language = ' javascript' ;
207209cbElm .label = ' your label' ;
208210cbElm .value = ` const hoge = true;
209211
210212console.log(hoge);` ;
211213
212-
213214// 6. Append
214215document .body .append (cbElm); // Render at the same time
215216` ` `
0 commit comments