(helpFile: HelpFile)
| 671 | // only contains modifications that are independent of the webview panel |
| 672 | // (i.e. no modified file paths, scroll position etc.) |
| 673 | function pimpMyHelp(helpFile: HelpFile): HelpFile { |
| 674 | // Retun if the help file is already modified |
| 675 | if (helpFile.isModified) { |
| 676 | return helpFile; |
| 677 | } |
| 678 | |
| 679 | // store original html content |
| 680 | helpFile.html0 = helpFile.html; |
| 681 | |
| 682 | // Make sure the helpfile content is actually html |
| 683 | const re = /^<!DOCTYPE html/; |
| 684 | const re2 = new RegExp('<html[^\\n]*>.*</html>', 'ms'); |
| 685 | helpFile.isHtml = (!!re.exec(helpFile.html) || !!re2.exec(helpFile.html)); |
| 686 | if (!helpFile.isHtml) { |
| 687 | const html = escapeHtml(helpFile.html); |
| 688 | helpFile.html = `<html><head></head><body><pre>${html}</pre></body></html>`; |
| 689 | helpFile.isModified = true; |
| 690 | } |
| 691 | |
| 692 | // parse the html string for futher modifications |
| 693 | const $ = cheerio.load(helpFile.html); |
| 694 | |
| 695 | // use .isHtml as proxy for syntax highlighting, clickable <pre> etc. |
| 696 | if(helpFile.isHtml){ |
| 697 | // Remove style elements specified in the html itself (replaced with custom CSS) |
| 698 | $('head style').remove(); |
| 699 | |
| 700 | // strip tags: <code class="language-R">... |
| 701 | const preCodes = $('pre>code'); |
| 702 | preCodes.each((_, pc) => { |
| 703 | $(pc).replaceWith($(pc).html() || ''); |
| 704 | }); |
| 705 | |
| 706 | // Split code examples at empty lines: |
| 707 | const codeClickConfig = config().get<CodeClickConfig>('helpPanel.clickCodeExamples'); |
| 708 | const isEnabled = CODE_CLICKS.some(k => codeClickConfig?.[k] !== 'Ignore'); |
| 709 | if(isEnabled){ |
| 710 | $('body').addClass('preClickable'); |
| 711 | const codeSections = $('pre'); |
| 712 | codeSections.each((i, section) => { |
| 713 | const innerHtml = $(section).html(); |
| 714 | if(!innerHtml){ |
| 715 | return; |
| 716 | } |
| 717 | const newPres = innerHtml.split('\n\n').map(s => s && `<pre class=preCodeExample>${s}</pre>`); |
| 718 | const newHtml = '<div class="preDiv">' + newPres.join('\n') + '</div>'; |
| 719 | $(section).replaceWith(newHtml); |
| 720 | }); |
| 721 | } |
| 722 | if(codeClickConfig?.Click !== 'Ignore'){ |
| 723 | $('body').addClass('preHoverPointer'); |
| 724 | } |
| 725 | |
| 726 | // Apply syntax highlighting: |
| 727 | if (config().get<boolean>('helpPanel.enableSyntaxHighlighting')) { |
| 728 | // find all code sections, enclosed by <pre>...</pre> |
| 729 | const codeSections = $('pre'); |
| 730 |
no test coverage detected