Add a language indicator to code snippets

master
Araozu 2024-05-28 16:35:24 -05:00
parent 946dbd6098
commit 4dc0b8524f
2 changed files with 17 additions and 11 deletions

View File

@ -23,9 +23,23 @@ export function highlightOnDom() {
pre_parent.removeChild(el);
pre_parent.appendChild(new_div);
// Create and append a language name indicator
CodeJar(new_div, thp_highlighter, {
tab: " ",
}).updateCode(code);
}
const pre_elements = document.querySelectorAll("pre");
for (const pre_el of pre_elements) {
const language = pre_el.getAttribute("data-language");
if (language === null) { continue; }
// Create a visual indicador
const indicator = document.createElement("span");
indicator.className = "absolute top-2 right-2 inline-block text-sm select-none opacity-75";
indicator.innerText = language;
pre_el.appendChild(indicator);
}
}

View File

@ -17,24 +17,16 @@ objects or other modules.
For example, to find if a string contains another, in PHP you'd do:
```php
<?php
if (str_contains("abc", "a")) {
// ...
}
```
In THP there is no `str_contains` function. Instead, you'd use the
`contains` function inside `String`:
In THP there is no `str_contains` function. Instead, you'd call the
`contains` method on the string:
```thp
if String.contains("abc", "a")
{
// ...
}
```
Or even better, you'd use the function as a method:
```thp data-thp
if "abc".contains("a")
{
// ...