### Highlight C# Code Tokens and Add Line Numbers (JavaScript) Source: https://docs.sisk-framework.org/docs/advanced/http-server-handlers These JavaScript functions are designed to process and enhance code blocks, particularly those written in C#. They identify and style specific C# tokens (classes, values, interfaces) and add visible line numbers to improve code comprehension. This functionality is crucial for presenting code examples effectively. ```javascript function runPostHljsFunctions() { function highlightMissingCodeTokens(pre) { const tokenClasses = [ "ByteArrayContent", "ForwardingResolver", "IPAddress", "IPEndPoint", "HttpServerExecutionResult", "ArgumentNullException", "JsonSerializerOptions", "DbContext" ]; const tokenValues = [ "RouteMethod", "Guid", "RequestHandlerExecutionMode", "HttpStatusCode", "HttpStatusInformation", "DateTime", "TimeSpan", "RouterMethod", "ListeningPort" ]; const tokenInterfaces = [ "IRequestHandler", "IEnumerable", "ICollection", "IList" ]; function runStyles(node) { if (node.nodeType === 3) { applyStyles(node); } else if (node.nodeType === 1) { const prohibitedClasses = ["hljs-comment", "hljs-string"]; if (! prohibitedClasses.some(cls => node.classList.contains(cls))) { for(const child of node.childNodes) { runStyles(child); } } } } function applyStyles(textNode) { const text = textNode.textContent; const fragment = []; for (const token of splitText(text, [...tokenClasses, ...tokenValues, ...tokenInterfaces])) { if (tokenClasses.includes(token)) { fragment.push(el("span.hljs-meta", token)); } else if (tokenValues.includes(token)) { fragment.push(el("span.hljs-meta-value", token)); } else if (tokenInterfaces.includes(token)) { fragment.push(el("span.hljs-meta-interface", token)); } else { fragment.push(token); } } textNode.replaceWith(el.fragment(...fragment)); } const code = pre.querySelector("code"); if (code && (code.classList.contains("lang-csharp") || code.classList.contains("lang-cs"))) { runStyles(code); } } function addLineNumbers(pre) { const code = pre.querySelector("code"); if (!code) return; var lines = (code.textContent.match(/\n/g) || []).length; if (lines <= 1) { return; } const lineElements = []; for (let i = 1; i <= lines; i++) { lineElements.push(el("span.hljs-line-number", i + "\n")); } code.prepend(el("div.line-numbers", ...lineElements)); code.classList.add("has-line-numbers"); } document.querySelectorAll("pre").forEach(pre => { highlightMissingCodeTokens(pre); addLineNumbers(pre); }); } runPostHljsFunctions(); ``` -------------------------------- ### Initialize Post-Highlighting Functions in Sisk Framework Source: https://docs.sisk-framework.org/docs/extensions/basic-auth This JavaScript snippet, `runPostHljsFunctions`, orchestrates the application of code highlighting and line numbering to all `
` elements on a page. It selects all `pre` elements and iterates through them, applying both `highlightMissingCodeTokens` and `addLineNumbers` to each. This ensures that code blocks are properly formatted and enhanced for better readability after initial syntax highlighting has been performed.
```javascript
function runPostHljsFunctions() {
document.querySelectorAll("pre").forEach(pre => {
highlightMissingCodeTokens(pre);
addLineNumbers(pre);
});
}
```
--------------------------------
### Execute Post-Highlighting JavaScript Functions
Source: https://docs.sisk-framework.org/docs/advanced/forwarding-resolvers
This JavaScript code snippet demonstrates the execution of post-syntax-highlighting functions within the Sisk framework. It iterates through all `pre` elements on the page, applying both `highlightMissingCodeTokens` and `addLineNumbers` to each. This ensures that C# code blocks are correctly processed for enhanced readability.
```javascript
document.querySelectorAll("pre").forEach(pre => {
highlightMissingCodeTokens(pre);
addLineNumbers(pre);
});
}
runPostHljsFunctions();
```
--------------------------------
### Apply Code Enhancements (JavaScript)
Source: https://docs.sisk-framework.org/docs/extensions/ini-configuration
This main execution block iterates through all `` elements on the page, applying the previously defined code enhancement functions: `highlightMissingCodeTokens` and `addLineNumbers`. This ensures that all code blocks are processed for improved readability. This script is intended to be run after the DOM is loaded and potentially after initial syntax highlighting.
```javascript
document.querySelectorAll("pre").forEach(pre => {
highlightMissingCodeTokens(pre);
addLineNumbers(pre);
});
```
--------------------------------
### Execute Post-Highlighting Functions
Source: https://docs.sisk-framework.org/docs/extensions/json-rpc
This script orchestrates the execution of post-syntax-highlighting functions, specifically `highlightMissingCodeTokens` and `addLineNumbers`, for all `pre` elements on the page. It ensures code blocks are enhanced consistently across the application.
```javascript
function runPostHljsFunctions() {
document.querySelectorAll("pre").forEach(pre => {
highlightMissingCodeTokens(pre);
addLineNumbers(pre);
});
}
runPostHljsFunctions();
```
--------------------------------
### JavaScript Post-Highlighting Function Execution
Source: https://docs.sisk-framework.org/docs/extensions/ssl-proxy
This JavaScript snippet executes utility functions after syntax highlighting has been applied to code blocks. It iterates through all `pre` elements on the page, applying custom token highlighting and line numbering to each.
```javascript
function runPostHljsFunctions() {
document.querySelectorAll("pre").forEach(pre => {
highlightMissingCodeTokens(pre);
addLineNumbers(pre);
});
}
runPostHljsFunctions();
```
--------------------------------
### Apply Code Highlighting and Line Numbers
Source: https://docs.sisk-framework.org/docs/faq
This JavaScript code iterates through all `` elements on the page. For each `` element, it first applies the `highlightMissingCodeTokens` function (which includes the C# token highlighting logic) and then the `addLineNumbers` function. This ensures that all code blocks on the page are properly formatted with syntax highlighting and line numbers.
```javascript
document.querySelectorAll("pre").forEach(pre => {
highlightMissingCodeTokens(pre);
addLineNumbers(pre);
});
}
runPostHljsFunctions();
```
--------------------------------
### Highlight C# Code Tokens and Add Line Numbers (JavaScript)
Source: https://docs.sisk-framework.org/docs/getting-started
This script iterates through 'pre' elements, identifies C# code blocks, highlights specific tokens like classes, values, and interfaces, and adds line numbers to each line for better readability. It uses predefined token lists and DOM manipulation.
```javascript
function runPostHljsFunctions() {
function highlightMissingCodeTokens(pre) {
const tokenClasses = [
"ByteArrayContent", "ForwardingResolver", "IPAddress", "IPEndPoint",
"HttpServerExecutionResult", "ArgumentNullException", "JsonSerializerOptions",
"DbContext"
];
const tokenValues = [
"RouteMethod", "Guid", "RequestHandlerExecutionMode", "HttpStatusCode",
"HttpStatusInformation", "DateTime", "TimeSpan", "RouterMethod",
"ListeningPort"
];
const tokenInterfaces = [
"IRequestHandler", "IEnumerable", "ICollection", "IList"
];
function runStyles(node) {
if (node.nodeType === 3) {
applyStyles(node);
} else if (node.nodeType === 1) {
const prohibitedClasses = ["hljs-comment", "hljs-string"];
if (! prohibitedClasses.some(cls => node.classList.contains(cls))) {
for(const child of node.childNodes) {
runStyles(child);
}
}
}
}
function applyStyles(textNode) {
const text = textNode.textContent;
const fragment = [];
for (const token of splitText(text, [...tokenClasses, ...tokenValues, ...tokenInterfaces])) {
if (tokenClasses.includes(token)) {
fragment.push(el("span.hljs-meta", token));
} else if (tokenValues.includes(token)) {
fragment.push(el("span.hljs-meta-value", token));
} else if (tokenInterfaces.includes(token)) {
fragment.push(el("span.hljs-meta-interface", token));
} else {
fragment.push(token);
}
}
textNode.replaceWith(el.fragment(...fragment));
}
const code = pre.querySelector("code");
if (code && (code.classList.contains("lang-csharp") || code.classList.contains("lang-cs"))) {
runStyles(code);
}
}
function addLineNumbers(pre) {
const code = pre.querySelector("code");
if (!code) return;
var lines = (code.textContent.match(/\n/g) || []).length;
if (lines <= 1) {
return;
}
const lineElements = [];
for (let i = 1; i <= lines; i++) {
lineElements.push(el("span.hljs-line-number", i + "\n"));
}
code.prepend(el("div.line-numbers", ...lineElements));
code.classList.add("has-line-numbers");
}
document.querySelectorAll("pre").forEach(pre => {
highlightMissingCodeTokens(pre);
addLineNumbers(pre);
});
}
runPostHljsFunctions();
```
--------------------------------
### Highlight C# Code Tokens and Add Line Numbers
Source: https://docs.sisk-framework.org/docs/advanced/manual-setup
This JavaScript code enhances code blocks by identifying and styling specific C# tokens (classes, values, interfaces) and adding line numbers. It iterates through all 'pre' elements on the page, applies token highlighting if the code is C#, and then adds line numbers for better code navigation. It relies on a helper function `el` for DOM element creation and `splitText` for tokenization.
```javascript
function runPostHljsFunctions() {
const tokenClasses = [
"ByteArrayContent", "ForwardingResolver", "IPAddress", "IPEndPoint",
"HttpServerExecutionResult", "ArgumentNullException", "JsonSerializerOptions",
"DbContext"
];
const tokenValues = [
"RouteMethod", "Guid", "RequestHandlerExecutionMode", "HttpStatusCode",
"HttpStatusInformation", "DateTime", "TimeSpan", "RouterMethod",
"ListeningPort"
];
const tokenInterfaces = [
"IRequestHandler", "IEnumerable", "ICollection", "IList"
];
function runStyles(node) {
if (node.nodeType === 3) {
applyStyles(node);
} else if (node.nodeType === 1) {
const prohibitedClasses = ["hljs-comment", "hljs-string"];
if (! prohibitedClasses.some(cls => node.classList.contains(cls))) {
for(const child of node.childNodes) {
runStyles(child);
}
}
}
}
function applyStyles(textNode) {
const text = textNode.textContent;
const fragment = [];
for (const token of splitText(text, [...tokenClasses, ...tokenValues, ...tokenInterfaces])) {
if (tokenClasses.includes(token)) {
fragment.push(el("span.hljs-meta", token));
} else if (tokenValues.includes(token)) {
fragment.push(el("span.hljs-meta-value", token));
} else if (tokenInterfaces.includes(token)) {
fragment.push(el("span.hljs-meta-interface", token));
} else {
fragment.push(token);
}
}
textNode.replaceWith(el.fragment(...fragment));
}
const code = pre.querySelector("code");
if (code && (code.classList.contains("lang-csharp") || code.classList.contains("lang-cs"))) {
runStyles(code);
}
}
function addLineNumbers(pre) {
const code = pre.querySelector("code");
if (!code) return;
var lines = (code.textContent.match(/\n/g) || []).length;
if (lines <= 1) {
return;
}
const lineElements = [];
for (let i = 1; i <= lines; i++) {
lineElements.push(el("span.hljs-line-number", i + "\n"));
}
code.prepend(el("div.line-numbers", ...lineElements));
code.classList.add("has-line-numbers");
}
document.querySelectorAll("pre").forEach(pre => {
highlightMissingCodeTokens(pre);
addLineNumbers(pre);
});
}
runPostHljsFunctions();
```
--------------------------------
### Highlight C# Tokens with JavaScript for Sisk Framework
Source: https://docs.sisk-framework.org/docs/deploying
This JavaScript function, `highlightMissingCodeTokens`, scans C# code blocks and applies specific CSS classes to recognized tokens, enhancing their visual distinction. It identifies tokens belonging to predefined lists of classes, values, and interfaces, and inserts them into the DOM with appropriate styling. This function is designed to work with code blocks identified by the `lang-csharp` or `lang-cs` classes.
```javascript
function highlightMissingCodeTokens(pre) {
const tokenClasses = [
"ByteArrayContent", "ForwardingResolver", "IPAddress", "IPEndPoint",
"HttpServerExecutionResult", "ArgumentNullException", "JsonSerializerOptions",
"DbContext"
];
const tokenValues = [
"RouteMethod", "Guid", "RequestHandlerExecutionMode", "HttpStatusCode",
"HttpStatusInformation", "DateTime", "TimeSpan", "RouterMethod",
"ListeningPort"
];
const tokenInterfaces = [
"IRequestHandler", "IEnumerable", "ICollection", "IList"
];
function runStyles(node) {
if (node.nodeType === 3) {
applyStyles(node);
} else if (node.nodeType === 1) {
const prohibitedClasses = ["hljs-comment", "hljs-string"];
if (! prohibitedClasses.some(cls => node.classList.contains(cls))) {
for(const child of node.childNodes) {
runStyles(child);
}
}
}
}
function applyStyles(textNode) {
const text = textNode.textContent;
const fragment = [];
for (const token of splitText(text, [...tokenClasses, ...tokenValues, ...tokenInterfaces])) {
if (tokenClasses.includes(token)) {
fragment.push(el("span.hljs-meta", token));
} else if (tokenValues.includes(token)) {
fragment.push(el("span.hljs-meta-value", token));
} else if (tokenInterfaces.includes(token)) {
fragment.push(el("span.hljs-meta-interface", token));
} else {
fragment.push(token);
}
}
textNode.replaceWith(el.fragment(...fragment));
}
const code = pre.querySelector("code");
if (code && (code.classList.contains("lang-csharp") || code.classList.contains("lang-cs"))) {
runStyles(code);
}
}
```
--------------------------------
### Highlight C# Code Tokens and Add Line Numbers
Source: https://docs.sisk-framework.org/docs/ssl
This function processes 'pre' elements to enhance C# code blocks. It identifies and styles predefined C# tokens (like 'ByteArrayContent', 'IPAddress', 'IEnumerable') using specific CSS classes. Additionally, it calculates the number of lines in a code block and prepends line numbers for improved readability. This is intended for use after initial syntax highlighting (e.g., by highlight.js).
```javascript
function runPostHljsFunctions() {
function highlightMissingCodeTokens(pre) {
const tokenClasses = [
"ByteArrayContent", "ForwardingResolver", "IPAddress", "IPEndPoint",
"HttpServerExecutionResult", "ArgumentNullException", "JsonSerializerOptions",
"DbContext"
];
const tokenValues = [
"RouteMethod", "Guid", "RequestHandlerExecutionMode", "HttpStatusCode",
"HttpStatusInformation", "DateTime", "TimeSpan", "RouterMethod",
"ListeningPort"
];
const tokenInterfaces = [
"IRequestHandler", "IEnumerable", "ICollection", "IList"
];
function runStyles(node) {
if (node.nodeType === 3) {
applyStyles(node);
} else if (node.nodeType === 1) {
const prohibitedClasses = ["hljs-comment", "hljs-string"];
if (! prohibitedClasses.some(cls => node.classList.contains(cls))) {
for (const child of node.childNodes) {
runStyles(child);
}
}
}
}
function applyStyles(textNode) {
const text = textNode.textContent;
const fragment = [];
for (const token of splitText(text, [...tokenClasses, ...tokenValues, ...tokenInterfaces])) {
if (tokenClasses.includes(token)) {
fragment.push(el("span.hljs-meta", token));
} else if (tokenValues.includes(token)) {
fragment.push(el("span.hljs-meta-value", token));
} else if (tokenInterfaces.includes(token)) {
fragment.push(el("span.hljs-meta-interface", token));
} else {
fragment.push(token);
}
}
textNode.replaceWith(el.fragment(...fragment));
}
const code = pre.querySelector("code");
if (code && (code.classList.contains("lang-csharp") || code.classList.contains("lang-cs"))) {
runStyles(code);
}
}
function addLineNumbers(pre) {
const code = pre.querySelector("code");
if (!code) return;
var lines = (code.textContent.match(/\n/g) || []).length;
if (lines <= 1) {
return;
}
const lineElements = [];
for (let i = 1; i <= lines; i++) {
lineElements.push(el("span.hljs-line-number", i + "\n"));
}
code.prepend(el("div.line-numbers", ...lineElements));
code.classList.add("has-line-numbers");
}
document.querySelectorAll("pre").forEach(pre => {
highlightMissingCodeTokens(pre);
addLineNumbers(pre);
});
}
runPostHljsFunctions();
```
--------------------------------
### Highlight C# Code Tokens with Sisk Framework
Source: https://docs.sisk-framework.org/docs/fundamentals/routing
This JavaScript function, `highlightMissingCodeTokens`, processes `pre` elements to identify and style specific C# tokens (classes, values, interfaces) within `code` blocks. It leverages predefined arrays of token types and uses DOM manipulation to apply distinct CSS classes for syntax highlighting. This function is intended for use after initial code highlighting (e.g., by highlight.js) to further refine the presentation of C# code.
```javascript
function highlightMissingCodeTokens(pre) {
const tokenClasses = [
"ByteArrayContent", "ForwardingResolver", "IPAddress", "IPEndPoint",
"HttpServerExecutionResult", "ArgumentNullException", "JsonSerializerOptions",
"DbContext"
];
const tokenValues = [
"RouteMethod", "Guid", "RequestHandlerExecutionMode", "HttpStatusCode",
"HttpStatusInformation", "DateTime", "TimeSpan", "RouterMethod",
"ListeningPort"
];
const tokenInterfaces = [
"IRequestHandler", "IEnumerable", "ICollection", "IList"
];
function runStyles(node) {
if (node.nodeType === 3) {
applyStyles(node);
} else if (node.nodeType === 1) {
const prohibitedClasses = ["hljs-comment", "hljs-string"];
if (! prohibitedClasses.some(cls => node.classList.contains(cls))) {
for(const child of node.childNodes) {
runStyles(child);
}
}
}
}
function applyStyles(textNode) {
const text = textNode.textContent;
const fragment = [];
for (const token of splitText(text, [...tokenClasses, ...tokenValues, ...tokenInterfaces])) {
if (tokenClasses.includes(token)) {
fragment.push(el("span.hljs-meta", token));
} else if (tokenValues.includes(token)) {
fragment.push(el("span.hljs-meta-value", token));
} else if (tokenInterfaces.includes(token)) {
fragment.push(el("span.hljs-meta-interface", token));
} else {
fragment.push(token);
}
}
textNode.replaceWith(el.fragment(...fragment));
}
const code = pre.querySelector("code");
if (code && (code.classList.contains("lang-csharp") || code.classList.contains("lang-cs"))) {
runStyles(code);
}
}
```
--------------------------------
### Highlight C# Code Tokens and Add Line Numbers
Source: https://docs.sisk-framework.org/docs/advanced/multi-host-setup
This JavaScript code targets 'pre' elements containing C# code. It defines arrays of known C# token types (classes, values, interfaces) and iterates through the code to apply specific CSS classes for highlighting. Additionally, it calculates the number of lines in the code block and prepends numbered spans to each line. This enhances the visual presentation of C# code on the web page.
```javascript
function runPostHljsFunctions() {
function highlightMissingCodeTokens(pre) {
const tokenClasses = [
"ByteArrayContent", "ForwardingResolver", "IPAddress", "IPEndPoint",
"HttpServerExecutionResult", "ArgumentNullException", "JsonSerializerOptions",
"DbContext"
];
const tokenValues = [
"RouteMethod", "Guid", "RequestHandlerExecutionMode", "HttpStatusCode",
"HttpStatusInformation", "DateTime", "TimeSpan", "RouterMethod",
"ListeningPort"
];
const tokenInterfaces = [
"IRequestHandler", "IEnumerable", "ICollection", "IList"
];
function runStyles(node) {
if (node.nodeType === 3) {
applyStyles(node);
} else if (node.nodeType === 1) {
const prohibitedClasses = ["hljs-comment", "hljs-string"];
if (! prohibitedClasses.some(cls => node.classList.contains(cls))) {
for(const child of node.childNodes) {
runStyles(child);
}
}
}
}
function applyStyles(textNode) {
const text = textNode.textContent;
const fragment = [];
for (const token of splitText(text, [...tokenClasses, ...tokenValues, ...tokenInterfaces])) {
if (tokenClasses.includes(token)) {
fragment.push(el("span.hljs-meta", token));
} else if (tokenValues.includes(token)) {
fragment.push(el("span.hljs-meta-value", token));
} else if (tokenInterfaces.includes(token)) {
fragment.push(el("span.hljs-meta-interface", token));
} else {
fragment.push(token);
}
}
textNode.replaceWith(el.fragment(...fragment));
}
const code = pre.querySelector("code");
if (code && (code.classList.contains("lang-csharp") || code.classList.contains("lang-cs"))) {
runStyles(code);
}
}
function addLineNumbers(pre) {
const code = pre.querySelector("code");
if (!code) return;
var lines = (code.textContent.match(/\n/g) || []).length;
if (lines <= 1) {
return;
}
const lineElements = [];
for (let i = 1; i <= lines; i++) {
lineElements.push(el("span.hljs-line-number", i + "\n"));
}
code.prepend(el("div.line-numbers", ...lineElements));
code.classList.add("has-line-numbers");
}
document.querySelectorAll("pre").forEach(pre => {
highlightMissingCodeTokens(pre);
addLineNumbers(pre);
});
}
runPostHljsFunctions();
```
--------------------------------
### JavaScript Syntax Highlighting for C# Tokens
Source: https://docs.sisk-framework.org/docs/extensions/ssl-proxy
This JavaScript code highlights specific C# tokens within code blocks. It identifies tokens like classes, values, and interfaces and applies corresponding CSS classes for styling. It relies on a helper function `splitText` and DOM manipulation.
```javascript
const tokenClasses = [
"ByteArrayContent", "ForwardingResolver", "IPAddress", "IPEndPoint",
"HttpServerExecutionResult", "ArgumentNullException", "JsonSerializerOptions",
"DbContext"
];
const tokenValues = [
"RouteMethod", "Guid", "RequestHandlerExecutionMode", "HttpStatusCode",
"HttpStatusInformation", "DateTime", "TimeSpan", "RouterMethod",
"ListeningPort"
];
const tokenInterfaces = [
"IRequestHandler", "IEnumerable", "ICollection", "IList"
];
function runStyles(node) {
if (node.nodeType === 3) {
applyStyles(node);
} else if (node.nodeType === 1) {
const prohibitedClasses = ["hljs-comment", "hljs-string"];
if (! prohibitedClasses.some(cls => node.classList.contains(cls))) {
for(const child of node.childNodes) {
runStyles(child);
}
}
}
}
function applyStyles(textNode) {
const text = textNode.textContent;
const fragment = [];
for (const token of splitText(text, [...tokenClasses, ...tokenValues, ...tokenInterfaces])) {
if (tokenClasses.includes(token)) {
fragment.push(el("span.hljs-meta", token));
} else if (tokenValues.includes(token)) {
fragment.push(el("span.hljs-meta-value", token));
} else if (tokenInterfaces.includes(token)) {
fragment.push(el("span.hljs-meta-interface", token));
} else {
fragment.push(token);
}
}
textNode.replaceWith(el.fragment(...fragment));
}
const code = pre.querySelector("code");
if (code && (code.classList.contains("lang-csharp") || code.classList.contains("lang-cs"))) {
runStyles(code);
}
```
--------------------------------
### Highlight Missing Code Tokens Logic in JavaScript
Source: https://docs.sisk-framework.org/docs/changelogs
A JavaScript function designed to run after code highlighting, specifically targeting and highlighting code tokens that might be missed by standard highlighting libraries. It iterates through predefined token classes to apply specific styling.
```javascript
function highlightMissingCodeTokens(pre) {
const tokenClasses = [
"HttpResponse", "HttpRequest", "File", "Task",
"Router", "Route", "StringContent", "StreamContent",
"JsonContent", "RegexRoute", "HtmlContent", "CancellationTokenSource",
"HttpContext", "Stream", "MultipartObject", "Thread", "Task", "Encoding",
"HttpKnownHeaderNames", "HttpMethod", "List", "JsonSerializer",
"LogStream", "HttpServer", "RotatingLogPolicy", "StringBuilder",
"Console", "HttpRequestEventSource", "HttpWebSocket", "X509Certificate2",
"AppDomain", "Path", "Directory", "HttpServerConfiguration", "ListeningHost"
];
// Logic to apply highlighting based on tokenClasses would go here
}
```
--------------------------------
### Highlight C# Code Tokens with JavaScript
Source: https://docs.sisk-framework.org/docs/extensions/service-providers
This function identifies and styles specific C# tokens (classes, values, interfaces) within code blocks. It utilizes predefined arrays of token types and iterates through the text nodes of a code element to apply distinct styles using `span` elements with specific classes.
```javascript
function runPostHljsFunctions() {
document.querySelectorAll("pre").forEach(pre => {
highlightMissingCodeTokens(pre);
addLineNumbers(pre);
});
}
function highlightMissingCodeTokens(pre) {
const tokenClasses = [
"ByteArrayContent", "ForwardingResolver", "IPAddress", "IPEndPoint",
"HttpServerExecutionResult", "ArgumentNullException", "JsonSerializerOptions",
"DbContext"
];
const tokenValues = [
"RouteMethod", "Guid", "RequestHandlerExecutionMode", "HttpStatusCode",
"HttpStatusInformation", "DateTime", "TimeSpan", "RouterMethod",
"ListeningPort"
];
const tokenInterfaces = [
"IRequestHandler", "IEnumerable", "ICollection", "IList"
];
function runStyles(node) {
if (node.nodeType === 3) {
applyStyles(node);
} else if (node.nodeType === 1) {
const prohibitedClasses = ["hljs-comment", "hljs-string"];
if (! prohibitedClasses.some(cls => node.classList.contains(cls))) {
for(const child of node.childNodes) {
runStyles(child);
}
}
}
}
function applyStyles(textNode) {
const text = textNode.textContent;
const fragment = [];
for (const token of splitText(text, [...tokenClasses, ...tokenValues, ...tokenInterfaces])) {
if (tokenClasses.includes(token)) {
fragment.push(el("span.hljs-meta", token));
} else if (tokenValues.includes(token)) {
fragment.push(el("span.hljs-meta-value", token));
} else if (tokenInterfaces.includes(token)) {
fragment.push(el("span.hljs-meta-interface", token));
} else {
fragment.push(token);
}
}
textNode.replaceWith(el.fragment(...fragment));
}
const code = pre.querySelector("code");
if (code && (code.classList.contains("lang-csharp") || code.classList.contains("lang-cs"))) {
runStyles(code);
}
}
function addLineNumbers(pre) {
const code = pre.querySelector("code");
if (!code) return;
var lines = (code.textContent.match(/\n/g) || []).length;
if (lines <= 1) {
return;
}
const lineElements = [];
for (let i = 1; i <= lines; i++) {
lineElements.push(el("span.hljs-line-number", i + "\n"));
}
code.prepend(el("div.line-numbers", ...lineElements));
code.classList.add("has-line-numbers");
}
runPostHljsFunctions();
```
--------------------------------
### Highlight C# Code Tokens and Add Line Numbers
Source: https://docs.sisk-framework.org/docs/features/server-sent-events
This JavaScript function processes code blocks, identifying and styling specific C# tokens (classes, values, interfaces) and adding line numbers for better readability. It utilizes helper functions like `splitText`, `el`, `applyStyles`, and `addLineNumbers` to achieve the desired highlighting.
```javascript
function highlightMissingCodeTokens(pre) {
const tokenClasses = [
"ByteArrayContent", "ForwardingResolver", "IPAddress", "IPEndPoint",
"HttpServerExecutionResult", "ArgumentNullException", "JsonSerializerOptions",
"DbContext"
];
const tokenValues = [
"RouteMethod", "Guid", "RequestHandlerExecutionMode", "HttpStatusCode",
"HttpStatusInformation", "DateTime", "TimeSpan", "RouterMethod",
"ListeningPort"
];
const tokenInterfaces = [
"IRequestHandler", "IEnumerable", "ICollection", "IList"
];
function runStyles(node) {
if (node.nodeType === 3) {
applyStyles(node);
} else if (node.nodeType === 1) {
const prohibitedClasses = ["hljs-comment", "hljs-string"];
if (! prohibitedClasses.some(cls => node.classList.contains(cls))) {
for(const child of node.childNodes) {
runStyles(child);
}
}
}
}
function applyStyles(textNode) {
const text = textNode.textContent;
const fragment = [];
for (const token of splitText(text, [...tokenClasses, ...tokenValues, ...tokenInterfaces])) {
if (tokenClasses.includes(token)) {
fragment.push(el("span.hljs-meta", token));
} else if (tokenValues.includes(token)) {
fragment.push(el("span.hljs-meta-value", token));
} else if (tokenInterfaces.includes(token)) {
fragment.push(el("span.hljs-meta-interface", token));
} else {
fragment.push(token);
}
}
textNode.replaceWith(el.fragment(...fragment));
}
const code = pre.querySelector("code");
if (code && (code.classList.contains("lang-csharp") || code.classList.contains("lang-cs"))) {
runStyles(code);
}
}
function addLineNumbers(pre) {
const code = pre.querySelector("code");
if (!code) return;
var lines = (code.textContent.match(/\n/g) || []).length;
if (lines <= 1) {
return;
}
const lineElements = [];
for (let i = 1; i <= lines; i++) {
lineElements.push(el("span.hljs-line-number", i + "\n"));
}
code.prepend(el("div.line-numbers", ...lineElements));
code.classList.add("has-line-numbers");
}
document.querySelectorAll("pre").forEach(pre => {
highlightMissingCodeTokens(pre);
addLineNumbers(pre);
});
```
--------------------------------
### JavaScript: Style Code Tokens and Add Line Numbers for Sisk Framework
Source: https://docs.sisk-framework.org/docs/features/discard-syntax
This snippet contains JavaScript functions for processing and styling code blocks. It identifies and highlights different token types (classes, values, interfaces) and adds line numbers to code elements. It is designed to work with pre-formatted code blocks, particularly those marked with 'lang-csharp' or 'lang-cs'.
```javascript
const tokenClasses = [
"ByteArrayContent", "ForwardingResolver", "IPAddress", "IPEndPoint",
"HttpServerExecutionResult", "ArgumentNullException", "JsonSerializerOptions",
"DbContext"
];
const tokenValues = [
"RouteMethod", "Guid", "RequestHandlerExecutionMode", "HttpStatusCode",
"HttpStatusInformation", "DateTime", "TimeSpan", "RouterMethod",
"ListeningPort"
];
const tokenInterfaces = [
"IRequestHandler", "IEnumerable", "ICollection", "IList"
];
function runStyles(node) {
if (node.nodeType === 3) {
applyStyles(node);
} else if (node.nodeType === 1) {
const prohibitedClasses = ["hljs-comment", "hljs-string"];
if (! prohibitedClasses.some(cls => node.classList.contains(cls))) {
for(const child of node.childNodes) {
runStyles(child);
}
}
}
}
function applyStyles(textNode) {
const text = textNode.textContent;
const fragment = [];
for (const token of splitText(text, [...tokenClasses, ...tokenValues, ...tokenInterfaces])) {
if (tokenClasses.includes(token)) {
fragment.push(el("span.hljs-meta", token));
} else if (tokenValues.includes(token)) {
fragment.push(el("span.hljs-meta-value", token));
} else if (tokenInterfaces.includes(token)) {
fragment.push(el("span.hljs-meta-interface", token));
} else {
fragment.push(token);
}
}
textNode.replaceWith(el.fragment(...fragment));
}
const code = pre.querySelector("code");
if (code && (code.classList.contains("lang-csharp") || code.classList.contains("lang-cs"))) {
runStyles(code);
}
}
function addLineNumbers(pre) {
const code = pre.querySelector("code");
if (!code) return;
var lines = (code.textContent.match(/\n/g) || []).length;
if (lines <= 1) {
return;
}
const lineElements = [];
for (let i = 1; i <= lines; i++) {
lineElements.push(el("span.hljs-line-number", i + "\n"));
}
code.prepend(el("div.line-numbers", ...lineElements));
code.classList.add("has-line-numbers");
}
document.querySelectorAll("pre").forEach(pre => {
highlightMissingCodeTokens(pre);
addLineNumbers(pre);
});
}
runPostHljsFunctions();
```
--------------------------------
### JavaScript Function to Highlight Code Tokens
Source: https://docs.sisk-framework.org/docs/features/content-streaming
This JavaScript function, intended to be run after syntax highlighting, identifies and highlights specific code tokens within preformatted code blocks. It recursively checks for code blocks and applies highlighting if a previous highlighting process has completed.
```javascript
function runPostHljsFunctions() {
if (!document.querySelector("pre>code")) {
return;
}
if (!document.querySelector(".hljs")) {
setTimeout(runPostHljsFunctions, 100);
return;
}
function highlightMissingCodeTokens(pre) {
const tokenClasses = [
"HttpResponse", "HttpRequest", "File", "Task",
"Router", "Route", "StringContent", "StreamContent",
"JsonContent", "RegexRoute", "HtmlContent", "CancellationTokenSource",
"HttpContext", "Stream", "MultipartObject", "Thread", "Task", "Encoding",
"HttpKnownHeaderNames", "HttpMethod", "List", "JsonSerializer",
"LogStream", "HttpServer", "RotatingLogPolicy", "StringBuilder",
"Console", "HttpRequestEventSource", "HttpWebSocket", "X509Certificate2",
"AppDomain", "Path", "Directory", "HttpServerConfiguration", "ListeningHost",
```
--------------------------------
### Highlight C# Code Tokens and Add Line Numbers
Source: https://docs.sisk-framework.org/docs/features/cors
This JavaScript code processes preformatted code blocks (specifically targeting C#) to apply custom styling to identified tokens and appends line numbers for improved readability. It relies on a helper function `el` for element creation and manipulates the DOM to inject these features.
```javascript
function highlightMissingCodeTokens(pre) {
const tokenClasses = [
"ByteArrayContent", "ForwardingResolver", "IPAddress", "IPEndPoint",
"HttpServerExecutionResult", "ArgumentNullException", "JsonSerializerOptions",
"DbContext"
];
const tokenValues = [
"RouteMethod", "Guid", "RequestHandlerExecutionMode", "HttpStatusCode",
"HttpStatusInformation", "DateTime", "TimeSpan", "RouterMethod",
"ListeningPort"
];
const tokenInterfaces = [
"IRequestHandler", "IEnumerable", "ICollection", "IList"
];
function runStyles(node) {
if (node.nodeType === 3) {
applyStyles(node);
} else if (node.nodeType === 1) {
const prohibitedClasses = ["hljs-comment", "hljs-string"];
if (! prohibitedClasses.some(cls => node.classList.contains(cls))) {
for(const child of node.childNodes) {
runStyles(child);
}
}
}
}
function applyStyles(textNode) {
const text = textNode.textContent;
const fragment = [];
for (const token of splitText(text, [...tokenClasses, ...tokenValues, ...tokenInterfaces])) {
if (tokenClasses.includes(token)) {
fragment.push(el("span.hljs-meta", token));
} else if (tokenValues.includes(token)) {
fragment.push(el("span.hljs-meta-value", token));
} else if (tokenInterfaces.includes(token)) {
fragment.push(el("span.hljs-meta-interface", token));
} else {
fragment.push(token);
}
}
textNode.replaceWith(el.fragment(...fragment));
}
const code = pre.querySelector("code");
if (code && (code.classList.contains("lang-csharp") || code.classList.contains("lang-cs"))) {
runStyles(code);
}
}
function addLineNumbers(pre) {
const code = pre.querySelector("code");
if (!code) return;
var lines = (code.textContent.match(/\n/g) || []).length;
if (lines <= 1) {
return;
}
const lineElements = [];
for (let i = 1; i <= lines; i++) {
lineElements.push(el("span.hljs-line-number", i + "\n"));
}
code.prepend(el("div.line-numbers", ...lineElements));
code.classList.add("has-line-numbers");
}
document.querySelectorAll("pre").forEach(pre => {
highlightMissingCodeTokens(pre);
addLineNumbers(pre);
});
}
runPostHljsFunctions();
```
--------------------------------
### Highlight Custom Code Tokens in C# Code
Source: https://docs.sisk-framework.org/docs/extensions/json-rpc
This JavaScript function, `highlightMissingCodeTokens`, processes C# code blocks. It identifies and styles predefined tokens (classes, values, interfaces) to improve readability. This function is intended for use after initial syntax highlighting has been applied.
```javascript
function highlightMissingCodeTokens(pre) {
const tokenClasses = [
"ByteArrayContent", "ForwardingResolver", "IPAddress", "IPEndPoint",
"HttpServerExecutionResult", "ArgumentNullException", "JsonSerializerOptions",
"DbContext"
];
const tokenValues = [
"RouteMethod", "Guid", "RequestHandlerExecutionMode", "HttpStatusCode",
"HttpStatusInformation", "DateTime", "TimeSpan", "RouterMethod",
"ListeningPort"
];
const tokenInterfaces = [
"IRequestHandler", "IEnumerable", "ICollection", "IList"
];
function runStyles(node) {
if (node.nodeType === 3) {
applyStyles(node);
} else if (node.nodeType === 1) {
const prohibitedClasses = ["hljs-comment", "hljs-string"];
if (! prohibitedClasses.some(cls => node.classList.contains(cls))) {
for(const child of node.childNodes) {
runStyles(child);
}
}
}
}
function applyStyles(textNode) {
const text = textNode.textContent;
const fragment = [];
for (const token of splitText(text, [...tokenClasses, ...tokenValues, ...tokenInterfaces])) {
if (tokenClasses.includes(token)) {
fragment.push(el("span.hljs-meta", token));
} else if (tokenValues.includes(token)) {
fragment.push(el("span.hljs-meta-value", token));
} else if (tokenInterfaces.includes(token)) {
fragment.push(el("span.hljs-meta-interface", token));
} else {
fragment.push(token);
}
}
textNode.replaceWith(el.fragment(...fragment));
}
const code = pre.querySelector("code");
if (code && (code.classList.contains("lang-csharp") || code.classList.contains("lang-cs"))) {
runStyles(code);
}
}
```
--------------------------------
### Highlight Missing Code Tokens Function in JavaScript
Source: https://docs.sisk-framework.org/docs/features/logging
A JavaScript function intended to highlight specific code tokens within provided HTML elements. It checks for the presence of code blocks and applies highlighting, with a mechanism to retry if highlighting is not immediately available. This function is designed to run after initial page rendering and potential syntax highlighting.
```javascript
function highlightMissingCodeTokens(pre) {
const tokenClasses = [
"HttpResponse", "HttpRequest", "File", "Task",
"Router", "Route", "StringContent", "StreamContent",
"JsonContent", "RegexRoute", "HtmlContent", "CancellationTokenSource",
"HttpContext", "Stream", "MultipartObject", "Thread", "Task", "Encoding",
"HttpKnownHeaderNames", "HttpMethod", "List", "JsonSerializer",
"LogStream", "HttpServer", "RotatingLogPolicy", "StringBuilder",
"Console", "HttpRequestEventSource", "HttpWebSocket", "X509Certificate2",
"AppDomain", "Path", "Directory", "HttpServerConfiguration", "ListeningHost"
];
// ... (rest of the function implementation)
```
--------------------------------
### Highlighting Missing Code Tokens in JavaScript
Source: https://docs.sisk-framework.org/docs/features/cors
This JavaScript function, `highlightMissingCodeTokens`, is designed to identify and potentially highlight specific code tokens within HTML elements that are typically used for code blocks. It iterates through a predefined list of common code-related tokens and applies highlighting logic. This function is likely part of a larger code highlighting or analysis tool.
```javascript
function highlightMissingCodeTokens(pre) {
const tokenClasses = [
"HttpResponse", "HttpRequest", "File", "Task",
"Router", "Route", "StringContent", "StreamContent",
"JsonContent", "RegexRoute", "HtmlContent", "CancellationTokenSource",
"HttpContext", "Stream", "MultipartObject", "Thread", "Task", "Encoding",
"HttpKnownHeaderNames", "HttpMethod", "List", "JsonSerializer",
"LogStream", "HttpServer", "RotatingLogPolicy", "StringBuilder",
"Console", "HttpRequestEventSource", "HttpWebSocket", "X509Certificate2",
"AppDomain", "Path", "Directory", "HttpServerConfiguration", "ListeningHost"
];
}
```
--------------------------------
### Add Line Numbers to Code Blocks
Source: https://docs.sisk-framework.org/docs/extensions/json-rpc
The `addLineNumbers` JavaScript function dynamically adds line numbers to code blocks. It counts the number of lines in a code element and prepends a numbered `span` for each line, enhancing code navigation and reference.
```javascript
function addLineNumbers(pre) {
const code = pre.querySelector("code");
if (!code) return;
var lines = (code.textContent.match(/\n/g) || []).length;
if (lines <= 1) {
return;
}
const lineElements = [];
for (let i = 1; i <= lines; i++) {
lineElements.push(el("span.hljs-line-number", i + "\n"));
}
code.prepend(el("div.line-numbers", ...lineElements));
code.classList.add("has-line-numbers");
}
```
--------------------------------
### Splitting Text by Words in JavaScript
Source: https://docs.sisk-framework.org/docs/features/cors
The `splitText` JavaScript function takes a string and an array of words as input. It escapes special characters within the provided words to ensure accurate pattern matching and then splits the input text based on these words using a case-insensitive regular expression. The function returns an array of text segments, excluding any empty segments resulting from the split.
```javascript
function splitText(text, words) {
if (!Array.isArray(words)) return [];
const escapedWords = words.map(word =>
word.replace(/([.*+?^${}()|[]\\])/g, '\\$1').replace(/\s/g, '\\$&'));
escapedWords.sort((a, b) => b.length - a.length || b.localeCompare(a, 'en-US', { sensitivity: 'base' }));
const pattern = new RegExp(`\\b(${escapedWords.join('|')})\\b`, 'gi');
const splitResult = text.split(pattern);
const cleanedResult = splitResult.filter(segment => segment !== '');
return cleanedResult;
}
```