Skip to content

Commit 921a731

Browse files
Add notrim attr
1 parent 6874971 commit 921a731

File tree

3 files changed

+46
-31
lines changed

3 files changed

+46
-31
lines changed

README.md

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# <code-block>
22

33
[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE) [![Published on NPM](https://img.shields.io/npm/v/@heppokofrontend/html-code-block-element.svg)](https://www.npmjs.com/package/@heppokofrontend/html-code-block-element) [![Published on webcomponents.org](https://img.shields.io/badge/webcomponents.org-published-blue.svg)](https://www.webcomponents.org/element/heppokofrontend/html-code-block-element) [![](https://data.jsdelivr.com/v1/package/npm/@heppokofrontend/html-code-block-element/badge)](https://www.jsdelivr.com/package/npm/@heppokofrontend/html-code-block-element) [![Maintainability](https://api.codeclimate.com/v1/badges/38a4e238adb7401844ba/maintainability)](https://codeclimate.com/github/heppokofrontend/html-code-block-element/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/38a4e238adb7401844ba/test_coverage)](https://codeclimate.com/github/heppokofrontend/html-code-block-element/test_coverage) [![Known Vulnerabilities](https://snyk.io/test/npm/@heppokofrontend/html-code-block-element/badge.svg)](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

66
Code 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-
&lt;script&gt;console.log(true);&lt;/script&gt;
28+
&lt;script&gt;console.log(true);&lt;/script&gt;
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
3536
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.
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

4249
There 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

5965
or
6066

6167
```html
6268
<code-block language="html" label="example.html" controls>
63-
&lt;script&gt;console.log(true);&lt;/script&gt;
69+
&lt;script&gt;console.log(true);&lt;/script&gt;
6470
</code-block>
6571
```
6672

@@ -78,6 +84,7 @@ or
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

131139
This 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
140148
import 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
165174
hljs.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
198202
customElements.define('code-block', HTMLCodeBlockElement);
199203

200-
201204
// 4. Make
202205
const cbElm = new HTMLCodeBlockElement();
203206

204-
205207
// 5. Assign
206208
cbElm.language = 'javascript';
207209
cbElm.label = 'your label';
208210
cbElm.value = `const hoge = true;
209211
210212
console.log(hoge);`;
211213

212-
213214
// 6. Append
214215
document.body.append(cbElm); // Render at the same time
215216
```

src/class/HTMLCodeBlockElement.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ export default class HTMLCodeBlockElement extends HTMLElement {
122122
/** Actual value of the accessor `controls` */
123123
#controls = false;
124124

125+
/** Actual value of the accessor `notrim` */
126+
#notrim = false;
127+
125128
/** Click event handler of copy button */
126129
#onClickButton = (() => {
127130
let key = -1;
@@ -171,13 +174,7 @@ export default class HTMLCodeBlockElement extends HTMLElement {
171174

172175
this.#observer.disconnect();
173176

174-
const src = (() => {
175-
if (/[^\n]\n$/.test(this.#value)) {
176-
return `${this.#value}\n`;
177-
}
178-
179-
return this.#value;
180-
})();
177+
const src = this.#notrim ? this.#value : this.#value.trim();
181178

182179
/** The resulting syntax-highlighted markup */
183180
const {markup} = HTMLCodeBlockElement.highlight({
@@ -294,8 +291,24 @@ export default class HTMLCodeBlockElement extends HTMLElement {
294291
this.#render();
295292
}
296293

294+
set notrim(value: boolean) {
295+
if (this.#notrim === value) {
296+
return;
297+
}
298+
299+
this.#notrim = value;
300+
301+
if (this.#notrim) {
302+
this.setAttribute('notrim', '');
303+
} else {
304+
this.removeAttribute('notrim');
305+
}
306+
307+
this.#render();
308+
}
309+
297310
static get observedAttributes(): string[] {
298-
return ['label', 'language', 'controls'];
311+
return ['label', 'language', 'controls', 'notrim'];
299312
}
300313

301314
attributeChangedCallback(
@@ -319,6 +332,7 @@ export default class HTMLCodeBlockElement extends HTMLElement {
319332

320333
// boolean
321334
case 'controls':
335+
case 'notrim':
322336
this[attrName] = typeof newValue === 'string';
323337
}
324338
}
@@ -416,6 +430,7 @@ export default class HTMLCodeBlockElement extends HTMLElement {
416430
this.#label = a11yName.textContent || '';
417431
this.#language = this.getAttribute('language') || '';
418432
this.#controls = this.getAttribute('controls') !== null;
433+
this.#notrim = this.getAttribute('notrim') !== null;
419434
this.#a11yName = a11yName;
420435
this.#copyButton = copyButton;
421436
this.#codeBlock = codeElm;

src/effects/add-style.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ style.textContent = `
2626
background: #75758a;
2727
box-sizing: border-box;
2828
}
29-
3029
code-block button {
3130
all: unset;
3231
outline: revert;

0 commit comments

Comments
 (0)