1/// <reference path="./showdown.d.ts" /> 2import showdown from "showdown"; 3var LATEX_IMAGE_URL = "https://latex.infourok.ru/?"; 4var HTML_ENTITY_PATTERN = RegExp("&(?:#\\d+|#x[0-9a-fA-F]+|[a-zA-Z][\\w:-]*);", "gu"); 5var ESCAPED_BLOCKQUOTE_MARKERS_PATTERN = /(^|\n)([ \t]{0,3})(?:>[ \t]?)+/g; 6var converter = null; 7var decodeEscapedEntitiesOneLevel = function(value) { 8 return value.replace(/&(#\d+|#x[0-9a-fA-F]+|[a-zA-Z][\w:-]*);/g, "&$1;"); 9}; 10var decodeHTMLEntitiesOneLevel = function(value) { 11 if (!value.includes("&")) { 12 return value; 13 } 14 var textarea = document.createElement("textarea"); 15 return value.replace(HTML_ENTITY_PATTERN, function(entity) { 16 textarea.innerHTML = entity; 17 return textarea.value; 18 }); 19}; 20var normalizeCodeEntities = function(html) { 21 if (!html.includes("&")) { 22 return html; 23 } 24 var template = document.createElement("template"); 25 template.innerHTML = html; 26 var codeNodes = template.content.querySelectorAll("code"); 27 codeNodes.forEach(function(_, index) { 28 var codeNode = codeNodes[index]; 29 var normalizedInnerHtml = decodeEscapedEntitiesOneLevel(codeNode.innerHTML); 30 codeNode.innerHTML = normalizedInnerHtml; 31 }); 32 return template.innerHTML; 33}; 34var wrapImagesWithLinks = function(html) { 35 if (!html.includes("<img")) { 36 return html; 37 } 38 var template = document.createElement("template"); 39 template.innerHTML = html; 40 template.content.querySelectorAll("img").forEach(function(image) { 41 var imageUrl = image.getAttribute("src"); 42 if (!imageUrl || image.closest("a")) { 43 return; 44 } 45 var link = document.createElement("a"); 46 link.href = imageUrl; 47 link.target = "_blank"; 48 link.rel = "noopener noreferrer"; 49 image.replaceWith(link); 50 link.append(image); 51 }); 52 return template.innerHTML; 53}; 54var normalizeTaskListStyles = function(html) { 55 if (!html.includes("task-list-item")) { 56 return html; 57 } 58 var template = document.createElement("template"); 59 template.innerHTML = html; 60 template.content.querySelectorAll("li.task-list-item").forEach(function(item) { 61 item.removeAttribute("style"); 62 }); 63 template.content.querySelectorAll('li.task-list-item input[type="checkbox"]').forEach(function(input) { 64 input.removeAttribute("style"); 65 }); 66 return template.innerHTML; 67}; 68var encodeRFC3986URIComponent = function(value) { 69 return encodeURIComponent(value).replace(/[!'()*]/g, function(character) { 70 return "%".concat(character.charCodeAt(0).toString(16).toUpperCase()); 71 }); 72}; 73var isCharacterEscaped = function(value, index) { 74 var backslashCount = 0; 75 for(var position = index - 1; position >= 0 && value[position] === "\\"; position -= 1){ 76 backslashCount += 1; 77 } 78 return backslashCount % 2 === 1; 79}; 80var unwrapLatexExpression = function(value) { 81 var depth = 0; 82 for(var i = 0; i < value.length; i += 1){ 83 var char = value[i]; 84 if ((char === "{" || char === "}") && !isCharacterEscaped(value, i)) { 85 depth += char === "{" ? 1 : -1; 86 if (depth === 0) { 87 return i === value.length - 1 ? value.slice(1, -1) : value; 88 } 89 } 90 } 91 return depth === 1 ? value.slice(1) : value; 92}; 93var escapeUnescapedPercents = function(value) { 94 return value.replace(RegExp("%", "gu"), function(character, index) { 95 return isCharacterEscaped(value, index) ? character : "\\".concat(character); 96 }); 97}; 98var normalizeLatexExpression = function(wrapper, expression) { 99 var sourceExpression = decodeHTMLEntitiesOneLevel(expression); 100 var value = escapeUnescapedPercents(decodeHTMLEntitiesOneLevel(sourceExpression).trim()); 101 if (!RegExp("^@latex\\s*\\{", "iu").test(wrapper) || !value.startsWith("{")) { 102 return value; 103 } 104 return unwrapLatexExpression(value).trim(); 105}; 106var replaceLatexWithMarkdownImage = function(text) { 107 return text.replace(RegExp("@latex\\s*([\\S\\s]+?)\\s*@endlatex", "giu"), function(match, expression) { 108 var value = normalizeLatexExpression(match, String(expression)); 109 if (!value) { 110 return match; 111 } 112 var encoded = encodeRFC3986URIComponent(value); 113 return ".concat(encoded, ")"); 114 }); 115}; 116var normalizeEscapedBlockquoteMarkers = function(text) { 117 return text.replace(ESCAPED_BLOCKQUOTE_MARKERS_PATTERN, function(match, linePrefix, indentation) { 118 var markerText = match.slice(linePrefix.length + indentation.length);
119 return "".concat(linePrefix).concat(indentation).concat(markerText.replace(/>/g, ">")); 120 }); 121}; 122var getConverter = function() { 123 if (converter) { 124 return converter; 125 } 126 converter = new showdown.Converter({ 127 tables: true, 128 strikethrough: true, 129 tasklists: true, 130 ghCodeBlocks: true, 131 smoothLivePreview: true, 132 openLinksInNewWindow: true, 133 disableForced4SpacesIndentedSublists: true, 134 splitAdjacentBlockquotes: true 135 }); 136 return converter; 137}; 138export var renderStreamingMarkdown = function(source) { 139 var rawText = source !== null && source !== void 0 ? source : ""; 140 if (rawText.trim() === "") { 141 return ""; 142 } 143 var markdownText = replaceLatexWithMarkdownImage(normalizeEscapedBlockquoteMarkers(rawText)); 144 var html = getConverter().makeHtml(markdownText); 145 return normalizeTaskListStyles(wrapImagesWithLinks(normalizeCodeEntities(html))); 146};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.