@@ -9,7 +9,7 @@ function cleanMathExpression(expression) {
99 // Replace \right } with \right)
1010 expression = expression . replace ( / \\ r i g h t \\ } / g, '\\right)' ) ;
1111 return expression ;
12- }
12+ }
1313marked . use ( "marked-extended-tables" , "gfm" ) ;
1414const renderer = new marked . Renderer ( ) ;
1515renderer . image = function ( href , title , text ) {
@@ -19,52 +19,50 @@ renderer.paragraph = function (text) {
1919 return `<p class="text-break"/>${ text } </p>` ;
2020} ;
2121marked . use ( { renderer } ) ;
22- // Override function
23- // const tokenizer = {
24- // codespan(src) {
25- // const match = src.match(/^\$+([^\$\n]+?)\$+/);
26- // if (match) {
27- // return {
28- // type: 'codespan',
29- // raw: match[0],
30- // text: match[1].trim()
31- // };
32- // }
33-
34- // // return false to use original codespan tokenizer
35- // return false;
36- // }
37- // };
38- // marked.use({ tokenizer });
39- // fetch("{{ resource_view.get('page_url') or resource.get('url') }}") // The path to the raw Markdown file
40- // .then(response => response.blob()) // Unwrap to a blob...
41- // .then(blob => blob.text()) // ...then to raw text...
42- // .then(markdown => { // ...then pass the raw text into marked.parse
43- // document.getElementById("markdown_content").innerHTML = DOMPurify.sanitize(marked.parse(markdown.replace(/^[\u200B\u200C\u200D\u200E\u200F\uFEFF]/, "")));
44- // });
45- const pageUrl = document . getElementById ( 'markdown_content' ) . getAttribute ( 'data-page-url' ) ;
46- fetch ( pageUrl ) // Path to raw Markdown file
47- . then ( response => response . blob ( ) ) // Unwrap to a blob...
48- . then ( blob => blob . text ( ) ) // Convert to text
49- . then ( markdown => {
50- // Sanitize and clean up the markdown text before parsing
51- // markdown = markdown.replace(/[^\x20-\x7E\x0A\x0D]/g, ''); // Remove non-printable characters
5222
23+
24+ const pageUrl = document . getElementById ( 'markdown_content' ) . getAttribute ( 'data-page-url' ) ;
25+ fetch ( pageUrl ) // Path to raw Markdown file
26+ . then ( response => response . blob ( ) ) // Unwrap to a blob...
27+ . then ( blob => blob . text ( ) ) // Convert to text
28+ . then ( markdown => {
5329 // Clean the markdown for math expressions
54- markdown = markdown . replace ( / \$ \$ [ \s \S ] * ?\$ \$ / g, match => cleanMathExpression ( match ) ) ; // Clean block math
55- markdown = markdown . replace ( / \$ [ \s \S ] * ?\$ / g, match => cleanMathExpression ( match ) ) ; // Clean inline math
56-
30+ // markdown = markdown.replace(/\$\$[\s\S]*?\$\$/g, match => cleanMathExpression(match)); // Clean block math
31+ // markdown = markdown.replace(/\$[\s\S]*?\$/g, match => cleanMathExpression(match)); // Clean inline math
5732 // Parsing the Markdown text into HTML
58- let parsedHTML = marked . parse ( markdown . replace ( / ^ [ \u200B \u200C \u200D \u200E \u200F \uFEFF ] / , "" ) ) ;
33+ // let parsedHTML = marked.parse(markdown.replace(/^[\u200B\u200C\u200D\u200E\u200F\uFEFF]/, ""));
34+ let parsedHTML ;
35+ // Überprüfen, ob start und end gesetzt und unterschiedlich sind
36+ if ( typeof start === 'number' && typeof end === 'number' && start < end ) {
37+ // Teile den Markdown-Text in drei Teile
38+ const beforeSlice = markdown . slice ( 0 , start ) ;
39+ const highlightedSlice = markdown . slice ( start , end ) ;
40+ const afterSlice = markdown . slice ( end ) ;
41+
42+ // Render die Teile
43+ let parsedBefore = marked . parse ( beforeSlice . replace ( / ^ [ \u200B \u200C \u200D \u200E \u200F \uFEFF ] / , "" ) ) ;
44+ let parsedHighlighted = `<div class="highlight">${ marked . parse ( highlightedSlice ) } </div>` ;
45+ let parsedAfter = marked . parse ( afterSlice . replace ( / ^ [ \u200B \u200C \u200D \u200E \u200F \uFEFF ] / , "" ) ) ;
5946
60- // Sanitize the generated HTML content
61- parsedHTML = DOMPurify . sanitize ( parsedHTML ) ;
47+ // Sanitize the generated HTML content
48+ parsedBefore = DOMPurify . sanitize ( parsedBefore ) ;
49+ parsedHighlighted = DOMPurify . sanitize ( parsedHighlighted ) ;
50+ parsedAfter = DOMPurify . sanitize ( parsedAfter ) ;
51+
52+ // Kombiniere die Teile
53+ parsedHTML = parsedBefore + parsedHighlighted + parsedAfter ;
54+ } else {
55+ // Wenn start und end nicht gültig sind, rendere den gesamten Markdown
56+ parsedHTML = marked . parse ( markdown . replace ( / ^ [ \u200B \u200C \u200D \u200E \u200F \uFEFF ] / , "" ) ) ;
57+ parsedHTML = DOMPurify . sanitize ( parsedHTML ) ;
58+ }
6259
6360 // Inject sanitized HTML into the page
6461 const contentDiv = document . getElementById ( "markdown_content" ) ;
6562 contentDiv . innerHTML = parsedHTML ;
66- } )
67- . catch ( error => console . error ( "Failed to load Markdown content:" , error ) ) ;
63+
64+ } )
65+ . catch ( error => console . error ( "Failed to load Markdown content:" , error ) ) ;
6866document . addEventListener ( "DOMContentLoaded" , function ( ) {
6967 //replace font location
7068 const styleSheets = document . styleSheets ;
0 commit comments