Skip to content

Commit

Permalink
refactor js_beautify to use a formatSource() fn
Browse files Browse the repository at this point in the history
  • Loading branch information
jmuzina committed Jul 26, 2024
1 parent 82205ab commit a77d292
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions templates/_layouts/docs.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ <h4 class="p-table-of-contents__header">On this page:</h4>
Prism.manual = true;
</script>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.15.1/beautify.min.js"></script>

Check warning

Code scanning / CodeQL

Inclusion of functionality from an untrusted source Medium

Script loaded from content delivery network with no integrity check.
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.15.1/beautify-css.min.js"></script>

Check warning

Code scanning / CodeQL

Inclusion of functionality from an untrusted source Medium

Script loaded from content delivery network with no integrity check.
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.15.1/beautify-html.min.js"></script>

Check warning

Code scanning / CodeQL

Inclusion of functionality from an untrusted source Medium

Script loaded from content delivery network with no integrity check.
<script defer src="{{ versioned_static('js/prism.min.js') }}"></script>
<script defer src="{{ versioned_static('js/example.js') }}"></script>
Expand Down
27 changes: 22 additions & 5 deletions templates/static/js/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,28 @@
request.send(null);
}

/**
* Format source code based on language
* @param {String} source - source code to format
* @param {String} lang - language of the source code
* @returns {String} formatted source code
*/
function formatSource(source, lang) {
switch (lang) {
case 'html':
return html_beautify(source, {indent_size: 2});
case 'js':
return js_beautify(source, {indent_size: 2});
case 'css':
return css_beautify(source, {indent_size: 2});
default:
return source;
}
}

function createPreCode(source, lang) {
var code = document.createElement('code');
code.appendChild(document.createTextNode(source));
code.appendChild(document.createTextNode(formatSource(source, lang)));

var pre = document.createElement('pre');
pre.classList.add('p-code-snippet__block');
Expand All @@ -78,7 +97,6 @@
}

function renderExample(placementElement, html) {
html = html_beautify(html);
var bodyPattern = /<body[^>]*>((.|[\n\r])*)<\/body>/im;
var titlePattern = /<title[^>]*>((.|[\n\r])*)<\/title>/im;
var headPattern = /<head[^>]*>((.|[\n\r])*)<\/head>/im;
Expand All @@ -87,11 +105,10 @@
var bodyHTML = bodyPattern.exec(html)[1].trim();
var headHTML = headPattern.exec(html)[1].trim();

var htmlSource = html_beautify(stripScriptsFromSource(bodyHTML));
var jsSource = js_beautify(getScriptFromSource(bodyHTML));
var htmlSource = stripScriptsFromSource(bodyHTML);
var jsSource = getScriptFromSource(bodyHTML);
var cssSource = getStyleFromSource(headHTML);
var externalScripts = getExternalScriptsFromSource(html);

var codePenData = {
html: htmlSource,
css: cssSource,
Expand Down

0 comments on commit a77d292

Please sign in to comment.