Skip to content

Commit 7066cfb

Browse files
⚡ Update Manual setup
1 parent 320f912 commit 7066cfb

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

README.md

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ import HTMLCodeBlockElement from 'html-code-block-element/dist/class/HTMLCodeBlo
114114
Manual setup:
115115

116116
```js
117+
// 1. Import
117118
import hljs from 'highlight.js/lib/core';
118119
import javascript from 'highlight.js/lib/languages/javascript';
119120
import HTMLCodeBlockElement from 'html-code-block-element/dist/class/HTMLCodeBlockElement';
@@ -122,13 +123,39 @@ import HTMLCodeBlockElement from 'html-code-block-element/dist/class/HTMLCodeBlo
122123
// Support JavaScript
123124
hljs.registerLanguage('javascript', javascript);
124125

125-
// Set endgine
126-
HTMLCodeBlockElement.endgine = hljs;
127126

128-
// Define
127+
// 2. Set endgine
128+
/**
129+
* Example: Callback to be called internally
130+
* @param {string} src - Source code string for highlight
131+
* @param {{ language: string }} options - Option for highlight
132+
* @returns {{ markup: string }} - Object of the highlight result
133+
*/
134+
HTMLCodeBlockElement.highlight = function (src, options) {
135+
if (
136+
// Verifying the existence of a language
137+
options?.language &&
138+
hljs.getLanguage(options.language)
139+
) {
140+
const {value} = hljs.highlight(
141+
src,
142+
options,
143+
);
144+
145+
return {
146+
markup: value,
147+
};
148+
}
149+
150+
return {
151+
markup: hljs.highlightAuto(src).value,
152+
};
153+
}
154+
155+
// 3. Define
129156
customElements.define('code-block', HTMLCodeBlockElement);
130157

131-
// Use
158+
// 4. Use
132159
const cbElm = new HTMLCodeBlockElement();
133160
```
134161

0 commit comments

Comments
 (0)