Skip to content

Commit c4ca25b

Browse files
authored
Merge pull request #2 from finsweet/code-blocks-titles
Code blocks titles
2 parents 789bfb3 + 2e4ef3a commit c4ca25b

File tree

6 files changed

+73
-15
lines changed

6 files changed

+73
-15
lines changed

_code-blocks/helpers.ts

+12
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,15 @@ export const createCopyButton = (id: string) => {
3131

3232
return copyButton;
3333
};
34+
35+
/**
36+
*
37+
* @param title
38+
* @returns
39+
*/
40+
export const createTitleElement = (title: string): HTMLDivElement => {
41+
const titleElement = document.createElement('div');
42+
titleElement.classList.add('codeTitle');
43+
titleElement.textContent = title;
44+
return titleElement;
45+
};

_code-blocks/webflow-theme.css

+17
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,20 @@ pre {
186186
transform: scale(0.98);
187187
background: #2b2b2b;
188188
}
189+
190+
/* Code Title */
191+
.codeTitle {
192+
position: absolute;
193+
left: 16px;
194+
top: 16px;
195+
right: auto;
196+
bottom: auto;
197+
display: -webkit-flex;
198+
display: -ms-flexbox;
199+
display: flex;
200+
font-family: monospace;
201+
font-size: 25px;
202+
font-weight: bold;
203+
color: #ff7f27;
204+
line-height: 1.25;
205+
}

_code-blocks/webflow/embed-code.js

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_code-blocks/webflow/embed-code.ts

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
import { createCopyButton, createTitleElement, populateCodeElement } from '../helpers';
12
import initCopyClipboard from '@finsweet/webflow-addons/copy-clipboard';
2-
import { createCopyButton, populateCodeElement } from '../helpers';
33
import highlightJS from '../hljs';
44

55
document.addEventListener('DOMContentLoaded', async () => {
66
// Get all the embed elements
77
const embedElements = document.querySelectorAll<HTMLDivElement>('.w-embed');
88

99
for (const [index, embedElement] of embedElements.entries()) {
10-
// Get the script source
11-
const src = embedElement.textContent;
12-
if (!src || !src.startsWith('http')) continue;
10+
// Get the script source and the title if exist
11+
let src = embedElement.textContent;
12+
13+
const titleRegex = /\[(.*)\]/;
14+
15+
if (!src || (!src.startsWith('http') && !titleRegex.test(src))) continue;
1316

1417
// Clear element's content
1518
embedElement.innerHTML = '';
@@ -21,6 +24,18 @@ document.addEventListener('DOMContentLoaded', async () => {
2124
codeElement.id = codeElementId;
2225
preElement.appendChild(codeElement);
2326

27+
// Get the title
28+
const titleMatch = titleRegex.exec(src);
29+
30+
if (titleMatch) {
31+
// Retrieve the title and sanitize the source URL
32+
const [brackedTitle, title] = titleMatch;
33+
src = src.replace(brackedTitle, '');
34+
35+
const titleElement = createTitleElement(title);
36+
preElement.appendChild(titleElement);
37+
}
38+
2439
// Populate the code element
2540
await populateCodeElement(src, codeElement);
2641

_code-blocks/zendesk/embed-code.js

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_code-blocks/zendesk/embed-code.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { createCopyButton, createTitleElement, populateCodeElement } from '../helpers';
12
import initCopyClipboard from '@finsweet/webflow-addons/copy-clipboard';
2-
import { createCopyButton, populateCodeElement } from '../helpers';
33
import highlightJS from '../hljs';
44

55
document.addEventListener('DOMContentLoaded', async () => {
@@ -8,8 +8,10 @@ document.addEventListener('DOMContentLoaded', async () => {
88

99
for (const [index, preElement] of preElements.entries()) {
1010
// Get the script source
11-
const src = preElement.textContent;
12-
if (!src || !src.startsWith('http')) continue;
11+
let src = preElement.textContent;
12+
const titleRegex = /\[(.*)\]/;
13+
14+
if (!src || (!src.startsWith('http') && !titleRegex.test(src))) continue;
1315

1416
// Clear element's content
1517
preElement.innerHTML = '';
@@ -20,6 +22,18 @@ document.addEventListener('DOMContentLoaded', async () => {
2022
codeElement.id = codeElementId;
2123
preElement.appendChild(codeElement);
2224

25+
// Get the title
26+
const titleMatch = titleRegex.exec(src);
27+
28+
if (titleMatch) {
29+
// Retrieve the title and sanitize the source URL
30+
const [brackedTitle, title] = titleMatch;
31+
src = src.replace(brackedTitle, '');
32+
33+
const titleElement = createTitleElement(title);
34+
preElement.appendChild(titleElement);
35+
}
36+
2337
// Populate the code element
2438
await populateCodeElement(src, codeElement);
2539

0 commit comments

Comments
 (0)