Latest commit

History

16 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Extract HTML code from HTML documents (showsource)

This library enables to extract HTML code from the current HTML document, and to add it to the current document in the form of source code.

The library is useful for showing the source code of the current HTML document, and its origin was to ease the creation of web pages for examples of HTML and CSS fragments. My problem was that I wanted to show the example and the code that generated it, but I did not want to maintain two separate code-fragments. This library solves this problem by extracting the HTML code from an element from the current document and adding it to the document in the form of source code.

Use case

The idea is that if we include the following code in the HTML document:

<divclass="example" data-showsource><h2>Simple example</h2><p><spanclass="fw-bold">hint:</span> add the <spanclass="fw-bold">data-showsource</span> attribute to the element that we want to extract the html content and to add the result after it.</p><p>the default behavior is to hide the root element (in this case, <spanclass="fw-bold">data-showsource</span>)</p><divclass="d-flex"><imgsrc="https://picsum.photos/400/200" class="mx-auto"></div></div>

The result will be as shown in the image below: we'll have a block with a title, the texts, and the image. By including the attribute data-showsource, in the envelope (i.e. the examplediv), we'll have the source code of the block after it.

Simple example

Once generated, we can use the library highlightjs to highlight the source code (but this library does not require it).

Usage

The library is available in the form of a CDN, so we can include it in our HTML document as follows:

<scriptsrc="https://cdn.jsdelivr.net/gh/dealfonso/showsource@1/dist/showsource.min.js"></script>

The library will discover any element with the data-showsource attribute and will add the source code of the element after it.

Declarative

The library can be used in a declarative way, by adding the data-showsource attribute to the element that we want to extract the HTML content and to add the result after it. A simple example is shown below:

<divclass="example" data-showsource><h2>Simple example</h2><divclass="d-flex"><imgsrc="https://picsum.photos/400/200" class="mx-auto"></div></div>

Javascript API

It is also possible to use the library from Javascript. So, we can use it to extract the HTML code from an element in a beautiful format (i.e. with indentation and line breaks). Then we can use the result to show it in a dialog, or to add it to the document in a different way.

The library will be available in the global variable showsource with the following methods:

  • showsource.extract(el, userOptions = {}, indent = "") - returns a list of strings with the HTML source code of the element el and its children. The userOptions parameter is optional and allows to customize the behavior of the function. The indent parameter is optional and is used to indent the source code.

  • showsource.show(selector="div[data-showsource]") - discovers any element with the given selector and adds the source code of the element in a div, after it. The function returns the list of elements that have been processed.

    This method is called automatically when the library is loaded, but it can be called again if we add new elements or if we want othe type of elements (e.g. showsource.show("div.example")).

An example of usage is shown below:

// get the element that we want to extract the html contentconstel=document.querySelector(".example");letlines=showsource.extract(el);// add the source code to the documentel.insertAdjacentHTML("afterend",`<pre><code>${lines.join("\n")}</code></pre>`);

Options

The showsource.extract method accepts an optional userOptions parameter that allows to customize the behavior of the function. The following options are available:

{// The indentation to be used (the spaces at the beginning of the line)indentation: " ",// Hide this element (does not affect to its children); data-showsource-hide or data-showsource-hide="true" to hide the elementhide: false,// Hide these elements (i.e. the tag) (does not affect to its children). The syntax is as in querySelector; data-showsource-hide-selector="h1,h2,h3,h4,h5,h6,p" to hide the child elements that match these selectorshideSelector: null,// Add to hide list (not overwrite the list) (*) created to be used in the declarative version in the data-showsource-hide-elements-add; has no sense in the programmatic version (not inherited)hideSelectorAdd: null,// Skip these elements (along with its children). The syntax is as in querySelector; data-showsource-skip-selector="h1,h2,h3,h4,h5,h6,p" to skip the element that match these selectorsskipSelector: null,// Add to skipSelector list (not overwrite the list) (*) created to be used in the declarative version in the data-showsource-skip-selector-add; has no sense in the programmatic version (not inherited)skipSelectorAdd: null,// Skip the children of the element, but not the element itself; data-showsource-skip-children="true" or simply data-showsource-skip-children to skip the childrenskipChildren: false,// Hide the attributes related to this plugin; data-showsource-hide-plugin="true" or simply data-showsource-hide-plugin to hide the attributes related to this pluginhidePlugin: true,// The attributes to be removed from the element; data-showsource-remove-attributes="class style" to remove these attributes from the elementremoveAttributes: null,// The class attribute to add to the main container div; data-showsource-class="showsource myclass" to add the classes "showsource" and "myclass" to the main container divclass: "showsource",// Number of characters to break the line of the tag; data-showsource-tag-line-break="80" to break the tag in a new line if the tag is longer than 80 characterstagLineBreak: null,// Max attributes number of consecutive attributes that may appear in a line; data-showsource-max-attributes-per-line="3" to break the attributes in a new line if there are more than 3 attributes in a rowmaxAttributesPerLine: null,// Space separated regular expressions that make that one attribute whose start matches any of them is rendered in a single line (i.e. no other attribute is rendered in the same line); e.g. if the value is data-* then all the attributes starting with data- will be rendered in a single lineseparateElements: null,}

The options can also be used in the declarative way, by adding the data-showsource-* attributes to the element that we want to extract the HTML content and to add the result after it.

The possible options are:

  • data-showsource-indentation is the indentation to be used (the spaces at the beginning of the line), that will be accumulated when rendering the children. The default value is two spaces, so that the source corresponding to the children of the element will have two extra spaces at the beginning. The value is inheritable to the children.
  • data-showsource-hide instructs the library to hide the render of the current element (i.e. the tag) itself, but not its children. The children will be rendered at the level of the current element. The default value is false.
  • data-showsource-hide-selector instructs the library to hide the render of the elements that match the selector (including the current element). The syntax is the same than using document.querySelector. The default value is null.
  • data-showsource-hide-selector-add is a list to be added to the list of selectors to hide. The syntax is the same than using document.querySelector. The default value is null. It has no sense in the programmatic version (not inherited).
  • data-showsource-skip instructs the library to skip the element, along with its children. The default value is false.
  • data-showsource-skip-selector instructs the library to skip the elements that match the selector, along with its children. The syntax is the same than using document.querySelector. The default value is null.
  • data-showsource-skip-selector-add is a list to be added to the list of selectors to skip. The syntax is the same than using document.querySelector. The default value is null. It has no sense in the programmatic version (not inherited).
  • data-showsource-skip-children instruct the library to the children of the element, but not the element itself. The default value is false.
  • data-showsource-hide-plugin instructs the library to hide the attributes related to this plugin (i.e. the data-showsource-* attributes). The default value is true. This attribute is inheritable to the children.
  • data-showsource-remove-attributes is a space separated list of the attributes to be hidden from the element. The default value is null.
  • data-showsource-class is a space separated list of classes to add to the main container div. The default value is showsource.
  • data-showsource-tag-line-break is the number of characters to break the line of the tag. The default value is null (i.e. deactivated). This attribute is inheritable to the children.
  • data-showsource-max-attributes-per-line is the max number of consecutive attributes that may appear in a line. The default value is null (i.e. deactivated). This attribute is inheritable to the children.
  • data-showsource-separate-elements is a space separated list of regular expressions that makes that one attribute that starts matching any of them is rendered in a single line with no other attribute in the same line; e.g. if the value is data-* then all the attributes starting with data- will be rendered in a single line. The default value is null (i.e. deactivated). This attribute is inheritable to the children.

Default options

The default options can be changed by modifying the showsource.defaults object. For example, to change the default indentation to four spaces, we can do the following after including the library:

showsource.defaults.indentation=" ";

To skip the elements that only add text, when using comments while showing how to use a library, we can do the following:

showsource.defaults.skipSelector="h1,h2,h3,h4,h5,h6,p";

About

Generates code snippets from the existing code in a web page. It is ideal to create pages with examples

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Latest commit

History

16 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Extract HTML code from HTML documents (showsource)

This library enables to extract HTML code from the current HTML document, and to add it to the current document in the form of source code.

The library is useful for showing the source code of the current HTML document, and its origin was to ease the creation of web pages for examples of HTML and CSS fragments. My problem was that I wanted to show the example and the code that generated it, but I did not want to maintain two separate code-fragments. This library solves this problem by extracting the HTML code from an element from the current document and adding it to the document in the form of source code.

Use case

The idea is that if we include the following code in the HTML document:

<divclass="example" data-showsource><h2>Simple example</h2><p><spanclass="fw-bold">hint:</span> add the <spanclass="fw-bold">data-showsource</span> attribute to the element that we want to extract the html content and to add the result after it.</p><p>the default behavior is to hide the root element (in this case, <spanclass="fw-bold">data-showsource</span>)</p><divclass="d-flex"><imgsrc="https://picsum.photos/400/200" class="mx-auto"></div></div>

The result will be as shown in the image below: we'll have a block with a title, the texts, and the image. By including the attribute data-showsource, in the envelope (i.e. the examplediv), we'll have the source code of the block after it.

Simple example

Once generated, we can use the library highlightjs to highlight the source code (but this library does not require it).

Usage

The library is available in the form of a CDN, so we can include it in our HTML document as follows:

<scriptsrc="https://cdn.jsdelivr.net/gh/dealfonso/showsource@1/dist/showsource.min.js"></script>

The library will discover any element with the data-showsource attribute and will add the source code of the element after it.

Declarative

The library can be used in a declarative way, by adding the data-showsource attribute to the element that we want to extract the HTML content and to add the result after it. A simple example is shown below:

<divclass="example" data-showsource><h2>Simple example</h2><divclass="d-flex"><imgsrc="https://picsum.photos/400/200" class="mx-auto"></div></div>

Javascript API

It is also possible to use the library from Javascript. So, we can use it to extract the HTML code from an element in a beautiful format (i.e. with indentation and line breaks). Then we can use the result to show it in a dialog, or to add it to the document in a different way.

The library will be available in the global variable showsource with the following methods:

  • showsource.extract(el, userOptions = {}, indent = "") - returns a list of strings with the HTML source code of the element el and its children. The userOptions parameter is optional and allows to customize the behavior of the function. The indent parameter is optional and is used to indent the source code.

  • showsource.show(selector="div[data-showsource]") - discovers any element with the given selector and adds the source code of the element in a div, after it. The function returns the list of elements that have been processed.

    This method is called automatically when the library is loaded, but it can be called again if we add new elements or if we want othe type of elements (e.g. showsource.show("div.example")).

An example of usage is shown below:

// get the element that we want to extract the html contentconstel=document.querySelector(".example");letlines=showsource.extract(el);// add the source code to the documentel.insertAdjacentHTML("afterend",`<pre><code>${lines.join("\n")}</code></pre>`);

Options

The showsource.extract method accepts an optional userOptions parameter that allows to customize the behavior of the function. The following options are available:

{// The indentation to be used (the spaces at the beginning of the line)indentation: " ",// Hide this element (does not affect to its children); data-showsource-hide or data-showsource-hide="true" to hide the elementhide: false,// Hide these elements (i.e. the tag) (does not affect to its children). The syntax is as in querySelector; data-showsource-hide-selector="h1,h2,h3,h4,h5,h6,p" to hide the child elements that match these selectorshideSelector: null,// Add to hide list (not overwrite the list) (*) created to be used in the declarative version in the data-showsource-hide-elements-add; has no sense in the programmatic version (not inherited)hideSelectorAdd: null,// Skip these elements (along with its children). The syntax is as in querySelector; data-showsource-skip-selector="h1,h2,h3,h4,h5,h6,p" to skip the element that match these selectorsskipSelector: null,// Add to skipSelector list (not overwrite the list) (*) created to be used in the declarative version in the data-showsource-skip-selector-add; has no sense in the programmatic version (not inherited)skipSelectorAdd: null,// Skip the children of the element, but not the element itself; data-showsource-skip-children="true" or simply data-showsource-skip-children to skip the childrenskipChildren: false,// Hide the attributes related to this plugin; data-showsource-hide-plugin="true" or simply data-showsource-hide-plugin to hide the attributes related to this pluginhidePlugin: true,// The attributes to be removed from the element; data-showsource-remove-attributes="class style" to remove these attributes from the elementremoveAttributes: null,// The class attribute to add to the main container div; data-showsource-class="showsource myclass" to add the classes "showsource" and "myclass" to the main container divclass: "showsource",// Number of characters to break the line of the tag; data-showsource-tag-line-break="80" to break the tag in a new line if the tag is longer than 80 characterstagLineBreak: null,// Max attributes number of consecutive attributes that may appear in a line; data-showsource-max-attributes-per-line="3" to break the attributes in a new line if there are more than 3 attributes in a rowmaxAttributesPerLine: null,// Space separated regular expressions that make that one attribute whose start matches any of them is rendered in a single line (i.e. no other attribute is rendered in the same line); e.g. if the value is data-* then all the attributes starting with data- will be rendered in a single lineseparateElements: null,}

The options can also be used in the declarative way, by adding the data-showsource-* attributes to the element that we want to extract the HTML content and to add the result after it.

The possible options are:

  • data-showsource-indentation is the indentation to be used (the spaces at the beginning of the line), that will be accumulated when rendering the children. The default value is two spaces, so that the source corresponding to the children of the element will have two extra spaces at the beginning. The value is inheritable to the children.
  • data-showsource-hide instructs the library to hide the render of the current element (i.e. the tag) itself, but not its children. The children will be rendered at the level of the current element. The default value is false.
  • data-showsource-hide-selector instructs the library to hide the render of the elements that match the selector (including the current element). The syntax is the same than using document.querySelector. The default value is null.
  • data-showsource-hide-selector-add is a list to be added to the list of selectors to hide. The syntax is the same than using document.querySelector. The default value is null. It has no sense in the programmatic version (not inherited).
  • data-showsource-skip instructs the library to skip the element, along with its children. The default value is false.
  • data-showsource-skip-selector instructs the library to skip the elements that match the selector, along with its children. The syntax is the same than using document.querySelector. The default value is null.
  • data-showsource-skip-selector-add is a list to be added to the list of selectors to skip. The syntax is the same than using document.querySelector. The default value is null. It has no sense in the programmatic version (not inherited).
  • data-showsource-skip-children instruct the library to the children of the element, but not the element itself. The default value is false.
  • data-showsource-hide-plugin instructs the library to hide the attributes related to this plugin (i.e. the data-showsource-* attributes). The default value is true. This attribute is inheritable to the children.
  • data-showsource-remove-attributes is a space separated list of the attributes to be hidden from the element. The default value is null.
  • data-showsource-class is a space separated list of classes to add to the main container div. The default value is showsource.
  • data-showsource-tag-line-break is the number of characters to break the line of the tag. The default value is null (i.e. deactivated). This attribute is inheritable to the children.
  • data-showsource-max-attributes-per-line is the max number of consecutive attributes that may appear in a line. The default value is null (i.e. deactivated). This attribute is inheritable to the children.
  • data-showsource-separate-elements is a space separated list of regular expressions that makes that one attribute that starts matching any of them is rendered in a single line with no other attribute in the same line; e.g. if the value is data-* then all the attributes starting with data- will be rendered in a single line. The default value is null (i.e. deactivated). This attribute is inheritable to the children.

Default options

The default options can be changed by modifying the showsource.defaults object. For example, to change the default indentation to four spaces, we can do the following after including the library:

showsource.defaults.indentation=" ";

To skip the elements that only add text, when using comments while showing how to use a library, we can do the following:

showsource.defaults.skipSelector="h1,h2,h3,h4,h5,h6,p";

About

Generates code snippets from the existing code in a web page. It is ideal to create pages with examples

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Latest commit

History

16 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Extract HTML code from HTML documents (showsource)

This library enables to extract HTML code from the current HTML document, and to add it to the current document in the form of source code.

The library is useful for showing the source code of the current HTML document, and its origin was to ease the creation of web pages for examples of HTML and CSS fragments. My problem was that I wanted to show the example and the code that generated it, but I did not want to maintain two separate code-fragments. This library solves this problem by extracting the HTML code from an element from the current document and adding it to the document in the form of source code.

Use case

The idea is that if we include the following code in the HTML document:

<divclass="example" data-showsource><h2>Simple example</h2><p><spanclass="fw-bold">hint:</span> add the <spanclass="fw-bold">data-showsource</span> attribute to the element that we want to extract the html content and to add the result after it.</p><p>the default behavior is to hide the root element (in this case, <spanclass="fw-bold">data-showsource</span>)</p><divclass="d-flex"><imgsrc="https://picsum.photos/400/200" class="mx-auto"></div></div>

The result will be as shown in the image below: we'll have a block with a title, the texts, and the image. By including the attribute data-showsource, in the envelope (i.e. the examplediv), we'll have the source code of the block after it.

Simple example

Once generated, we can use the library highlightjs to highlight the source code (but this library does not require it).

Usage

The library is available in the form of a CDN, so we can include it in our HTML document as follows:

<scriptsrc="https://cdn.jsdelivr.net/gh/dealfonso/showsource@1/dist/showsource.min.js"></script>

The library will discover any element with the data-showsource attribute and will add the source code of the element after it.

Declarative

The library can be used in a declarative way, by adding the data-showsource attribute to the element that we want to extract the HTML content and to add the result after it. A simple example is shown below:

<divclass="example" data-showsource><h2>Simple example</h2><divclass="d-flex"><imgsrc="https://picsum.photos/400/200" class="mx-auto"></div></div>

Javascript API

It is also possible to use the library from Javascript. So, we can use it to extract the HTML code from an element in a beautiful format (i.e. with indentation and line breaks). Then we can use the result to show it in a dialog, or to add it to the document in a different way.

The library will be available in the global variable showsource with the following methods:

  • showsource.extract(el, userOptions = {}, indent = "") - returns a list of strings with the HTML source code of the element el and its children. The userOptions parameter is optional and allows to customize the behavior of the function. The indent parameter is optional and is used to indent the source code.

  • showsource.show(selector="div[data-showsource]") - discovers any element with the given selector and adds the source code of the element in a div, after it. The function returns the list of elements that have been processed.

    This method is called automatically when the library is loaded, but it can be called again if we add new elements or if we want othe type of elements (e.g. showsource.show("div.example")).

An example of usage is shown below:

// get the element that we want to extract the html contentconstel=document.querySelector(".example");letlines=showsource.extract(el);// add the source code to the documentel.insertAdjacentHTML("afterend",`<pre><code>${lines.join("\n")}</code></pre>`);

Options

The showsource.extract method accepts an optional userOptions parameter that allows to customize the behavior of the function. The following options are available:

{// The indentation to be used (the spaces at the beginning of the line)indentation: " ",// Hide this element (does not affect to its children); data-showsource-hide or data-showsource-hide="true" to hide the elementhide: false,// Hide these elements (i.e. the tag) (does not affect to its children). The syntax is as in querySelector; data-showsource-hide-selector="h1,h2,h3,h4,h5,h6,p" to hide the child elements that match these selectorshideSelector: null,// Add to hide list (not overwrite the list) (*) created to be used in the declarative version in the data-showsource-hide-elements-add; has no sense in the programmatic version (not inherited)hideSelectorAdd: null,// Skip these elements (along with its children). The syntax is as in querySelector; data-showsource-skip-selector="h1,h2,h3,h4,h5,h6,p" to skip the element that match these selectorsskipSelector: null,// Add to skipSelector list (not overwrite the list) (*) created to be used in the declarative version in the data-showsource-skip-selector-add; has no sense in the programmatic version (not inherited)skipSelectorAdd: null,// Skip the children of the element, but not the element itself; data-showsource-skip-children="true" or simply data-showsource-skip-children to skip the childrenskipChildren: false,// Hide the attributes related to this plugin; data-showsource-hide-plugin="true" or simply data-showsource-hide-plugin to hide the attributes related to this pluginhidePlugin: true,// The attributes to be removed from the element; data-showsource-remove-attributes="class style" to remove these attributes from the elementremoveAttributes: null,// The class attribute to add to the main container div; data-showsource-class="showsource myclass" to add the classes "showsource" and "myclass" to the main container divclass: "showsource",// Number of characters to break the line of the tag; data-showsource-tag-line-break="80" to break the tag in a new line if the tag is longer than 80 characterstagLineBreak: null,// Max attributes number of consecutive attributes that may appear in a line; data-showsource-max-attributes-per-line="3" to break the attributes in a new line if there are more than 3 attributes in a rowmaxAttributesPerLine: null,// Space separated regular expressions that make that one attribute whose start matches any of them is rendered in a single line (i.e. no other attribute is rendered in the same line); e.g. if the value is data-* then all the attributes starting with data- will be rendered in a single lineseparateElements: null,}

The options can also be used in the declarative way, by adding the data-showsource-* attributes to the element that we want to extract the HTML content and to add the result after it.

The possible options are:

  • data-showsource-indentation is the indentation to be used (the spaces at the beginning of the line), that will be accumulated when rendering the children. The default value is two spaces, so that the source corresponding to the children of the element will have two extra spaces at the beginning. The value is inheritable to the children.
  • data-showsource-hide instructs the library to hide the render of the current element (i.e. the tag) itself, but not its children. The children will be rendered at the level of the current element. The default value is false.
  • data-showsource-hide-selector instructs the library to hide the render of the elements that match the selector (including the current element). The syntax is the same than using document.querySelector. The default value is null.
  • data-showsource-hide-selector-add is a list to be added to the list of selectors to hide. The syntax is the same than using document.querySelector. The default value is null. It has no sense in the programmatic version (not inherited).
  • data-showsource-skip instructs the library to skip the element, along with its children. The default value is false.
  • data-showsource-skip-selector instructs the library to skip the elements that match the selector, along with its children. The syntax is the same than using document.querySelector. The default value is null.
  • data-showsource-skip-selector-add is a list to be added to the list of selectors to skip. The syntax is the same than using document.querySelector. The default value is null. It has no sense in the programmatic version (not inherited).
  • data-showsource-skip-children instruct the library to the children of the element, but not the element itself. The default value is false.
  • data-showsource-hide-plugin instructs the library to hide the attributes related to this plugin (i.e. the data-showsource-* attributes). The default value is true. This attribute is inheritable to the children.
  • data-showsource-remove-attributes is a space separated list of the attributes to be hidden from the element. The default value is null.
  • data-showsource-class is a space separated list of classes to add to the main container div. The default value is showsource.
  • data-showsource-tag-line-break is the number of characters to break the line of the tag. The default value is null (i.e. deactivated). This attribute is inheritable to the children.
  • data-showsource-max-attributes-per-line is the max number of consecutive attributes that may appear in a line. The default value is null (i.e. deactivated). This attribute is inheritable to the children.
  • data-showsource-separate-elements is a space separated list of regular expressions that makes that one attribute that starts matching any of them is rendered in a single line with no other attribute in the same line; e.g. if the value is data-* then all the attributes starting with data- will be rendered in a single line. The default value is null (i.e. deactivated). This attribute is inheritable to the children.

Default options

The default options can be changed by modifying the showsource.defaults object. For example, to change the default indentation to four spaces, we can do the following after including the library:

showsource.defaults.indentation=" ";

To skip the elements that only add text, when using comments while showing how to use a library, we can do the following:

showsource.defaults.skipSelector="h1,h2,h3,h4,h5,h6,p";

About

Generates code snippets from the existing code in a web page. It is ideal to create pages with examples

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Latest commit

History

16 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Extract HTML code from HTML documents (showsource)

This library enables to extract HTML code from the current HTML document, and to add it to the current document in the form of source code.

The library is useful for showing the source code of the current HTML document, and its origin was to ease the creation of web pages for examples of HTML and CSS fragments. My problem was that I wanted to show the example and the code that generated it, but I did not want to maintain two separate code-fragments. This library solves this problem by extracting the HTML code from an element from the current document and adding it to the document in the form of source code.

Use case

The idea is that if we include the following code in the HTML document:

<divclass="example" data-showsource><h2>Simple example</h2><p><spanclass="fw-bold">hint:</span> add the <spanclass="fw-bold">data-showsource</span> attribute to the element that we want to extract the html content and to add the result after it.</p><p>the default behavior is to hide the root element (in this case, <spanclass="fw-bold">data-showsource</span>)</p><divclass="d-flex"><imgsrc="https://picsum.photos/400/200" class="mx-auto"></div></div>

The result will be as shown in the image below: we'll have a block with a title, the texts, and the image. By including the attribute data-showsource, in the envelope (i.e. the examplediv), we'll have the source code of the block after it.

Simple example

Once generated, we can use the library highlightjs to highlight the source code (but this library does not require it).

Usage

The library is available in the form of a CDN, so we can include it in our HTML document as follows:

<scriptsrc="https://cdn.jsdelivr.net/gh/dealfonso/showsource@1/dist/showsource.min.js"></script>

The library will discover any element with the data-showsource attribute and will add the source code of the element after it.

Declarative

The library can be used in a declarative way, by adding the data-showsource attribute to the element that we want to extract the HTML content and to add the result after it. A simple example is shown below:

<divclass="example" data-showsource><h2>Simple example</h2><divclass="d-flex"><imgsrc="https://picsum.photos/400/200" class="mx-auto"></div></div>

Javascript API

It is also possible to use the library from Javascript. So, we can use it to extract the HTML code from an element in a beautiful format (i.e. with indentation and line breaks). Then we can use the result to show it in a dialog, or to add it to the document in a different way.

The library will be available in the global variable showsource with the following methods:

  • showsource.extract(el, userOptions = {}, indent = "") - returns a list of strings with the HTML source code of the element el and its children. The userOptions parameter is optional and allows to customize the behavior of the function. The indent parameter is optional and is used to indent the source code.

  • showsource.show(selector="div[data-showsource]") - discovers any element with the given selector and adds the source code of the element in a div, after it. The function returns the list of elements that have been processed.

    This method is called automatically when the library is loaded, but it can be called again if we add new elements or if we want othe type of elements (e.g. showsource.show("div.example")).

An example of usage is shown below:

// get the element that we want to extract the html contentconstel=document.querySelector(".example");letlines=showsource.extract(el);// add the source code to the documentel.insertAdjacentHTML("afterend",`<pre><code>${lines.join("\n")}</code></pre>`);

Options

The showsource.extract method accepts an optional userOptions parameter that allows to customize the behavior of the function. The following options are available:

{// The indentation to be used (the spaces at the beginning of the line)indentation: " ",// Hide this element (does not affect to its children); data-showsource-hide or data-showsource-hide="true" to hide the elementhide: false,// Hide these elements (i.e. the tag) (does not affect to its children). The syntax is as in querySelector; data-showsource-hide-selector="h1,h2,h3,h4,h5,h6,p" to hide the child elements that match these selectorshideSelector: null,// Add to hide list (not overwrite the list) (*) created to be used in the declarative version in the data-showsource-hide-elements-add; has no sense in the programmatic version (not inherited)hideSelectorAdd: null,// Skip these elements (along with its children). The syntax is as in querySelector; data-showsource-skip-selector="h1,h2,h3,h4,h5,h6,p" to skip the element that match these selectorsskipSelector: null,// Add to skipSelector list (not overwrite the list) (*) created to be used in the declarative version in the data-showsource-skip-selector-add; has no sense in the programmatic version (not inherited)skipSelectorAdd: null,// Skip the children of the element, but not the element itself; data-showsource-skip-children="true" or simply data-showsource-skip-children to skip the childrenskipChildren: false,// Hide the attributes related to this plugin; data-showsource-hide-plugin="true" or simply data-showsource-hide-plugin to hide the attributes related to this pluginhidePlugin: true,// The attributes to be removed from the element; data-showsource-remove-attributes="class style" to remove these attributes from the elementremoveAttributes: null,// The class attribute to add to the main container div; data-showsource-class="showsource myclass" to add the classes "showsource" and "myclass" to the main container divclass: "showsource",// Number of characters to break the line of the tag; data-showsource-tag-line-break="80" to break the tag in a new line if the tag is longer than 80 characterstagLineBreak: null,// Max attributes number of consecutive attributes that may appear in a line; data-showsource-max-attributes-per-line="3" to break the attributes in a new line if there are more than 3 attributes in a rowmaxAttributesPerLine: null,// Space separated regular expressions that make that one attribute whose start matches any of them is rendered in a single line (i.e. no other attribute is rendered in the same line); e.g. if the value is data-* then all the attributes starting with data- will be rendered in a single lineseparateElements: null,}

The options can also be used in the declarative way, by adding the data-showsource-* attributes to the element that we want to extract the HTML content and to add the result after it.

The possible options are:

  • data-showsource-indentation is the indentation to be used (the spaces at the beginning of the line), that will be accumulated when rendering the children. The default value is two spaces, so that the source corresponding to the children of the element will have two extra spaces at the beginning. The value is inheritable to the children.
  • data-showsource-hide instructs the library to hide the render of the current element (i.e. the tag) itself, but not its children. The children will be rendered at the level of the current element. The default value is false.
  • data-showsource-hide-selector instructs the library to hide the render of the elements that match the selector (including the current element). The syntax is the same than using document.querySelector. The default value is null.
  • data-showsource-hide-selector-add is a list to be added to the list of selectors to hide. The syntax is the same than using document.querySelector. The default value is null. It has no sense in the programmatic version (not inherited).
  • data-showsource-skip instructs the library to skip the element, along with its children. The default value is false.
  • data-showsource-skip-selector instructs the library to skip the elements that match the selector, along with its children. The syntax is the same than using document.querySelector. The default value is null.
  • data-showsource-skip-selector-add is a list to be added to the list of selectors to skip. The syntax is the same than using document.querySelector. The default value is null. It has no sense in the programmatic version (not inherited).
  • data-showsource-skip-children instruct the library to the children of the element, but not the element itself. The default value is false.
  • data-showsource-hide-plugin instructs the library to hide the attributes related to this plugin (i.e. the data-showsource-* attributes). The default value is true. This attribute is inheritable to the children.
  • data-showsource-remove-attributes is a space separated list of the attributes to be hidden from the element. The default value is null.
  • data-showsource-class is a space separated list of classes to add to the main container div. The default value is showsource.
  • data-showsource-tag-line-break is the number of characters to break the line of the tag. The default value is null (i.e. deactivated). This attribute is inheritable to the children.
  • data-showsource-max-attributes-per-line is the max number of consecutive attributes that may appear in a line. The default value is null (i.e. deactivated). This attribute is inheritable to the children.
  • data-showsource-separate-elements is a space separated list of regular expressions that makes that one attribute that starts matching any of them is rendered in a single line with no other attribute in the same line; e.g. if the value is data-* then all the attributes starting with data- will be rendered in a single line. The default value is null (i.e. deactivated). This attribute is inheritable to the children.

Default options

The default options can be changed by modifying the showsource.defaults object. For example, to change the default indentation to four spaces, we can do the following after including the library:

showsource.defaults.indentation=" ";

To skip the elements that only add text, when using comments while showing how to use a library, we can do the following:

showsource.defaults.skipSelector="h1,h2,h3,h4,h5,h6,p";

About

Generates code snippets from the existing code in a web page. It is ideal to create pages with examples

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Latest commit

History

16 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Extract HTML code from HTML documents (showsource)

This library enables to extract HTML code from the current HTML document, and to add it to the current document in the form of source code.

The library is useful for showing the source code of the current HTML document, and its origin was to ease the creation of web pages for examples of HTML and CSS fragments. My problem was that I wanted to show the example and the code that generated it, but I did not want to maintain two separate code-fragments. This library solves this problem by extracting the HTML code from an element from the current document and adding it to the document in the form of source code.

Use case

The idea is that if we include the following code in the HTML document:

<divclass="example" data-showsource><h2>Simple example</h2><p><spanclass="fw-bold">hint:</span> add the <spanclass="fw-bold">data-showsource</span> attribute to the element that we want to extract the html content and to add the result after it.</p><p>the default behavior is to hide the root element (in this case, <spanclass="fw-bold">data-showsource</span>)</p><divclass="d-flex"><imgsrc="https://picsum.photos/400/200" class="mx-auto"></div></div>

The result will be as shown in the image below: we'll have a block with a title, the texts, and the image. By including the attribute data-showsource, in the envelope (i.e. the examplediv), we'll have the source code of the block after it.

Simple example

Once generated, we can use the library highlightjs to highlight the source code (but this library does not require it).

Usage

The library is available in the form of a CDN, so we can include it in our HTML document as follows:

<scriptsrc="https://cdn.jsdelivr.net/gh/dealfonso/showsource@1/dist/showsource.min.js"></script>

The library will discover any element with the data-showsource attribute and will add the source code of the element after it.

Declarative

The library can be used in a declarative way, by adding the data-showsource attribute to the element that we want to extract the HTML content and to add the result after it. A simple example is shown below:

<divclass="example" data-showsource><h2>Simple example</h2><divclass="d-flex"><imgsrc="https://picsum.photos/400/200" class="mx-auto"></div></div>

Javascript API

It is also possible to use the library from Javascript. So, we can use it to extract the HTML code from an element in a beautiful format (i.e. with indentation and line breaks). Then we can use the result to show it in a dialog, or to add it to the document in a different way.

The library will be available in the global variable showsource with the following methods:

  • showsource.extract(el, userOptions = {}, indent = "") - returns a list of strings with the HTML source code of the element el and its children. The userOptions parameter is optional and allows to customize the behavior of the function. The indent parameter is optional and is used to indent the source code.

  • showsource.show(selector="div[data-showsource]") - discovers any element with the given selector and adds the source code of the element in a div, after it. The function returns the list of elements that have been processed.

    This method is called automatically when the library is loaded, but it can be called again if we add new elements or if we want othe type of elements (e.g. showsource.show("div.example")).

An example of usage is shown below:

// get the element that we want to extract the html contentconstel=document.querySelector(".example");letlines=showsource.extract(el);// add the source code to the documentel.insertAdjacentHTML("afterend",`<pre><code>${lines.join("\n")}</code></pre>`);

Options

The showsource.extract method accepts an optional userOptions parameter that allows to customize the behavior of the function. The following options are available:

{// The indentation to be used (the spaces at the beginning of the line)indentation: " ",// Hide this element (does not affect to its children); data-showsource-hide or data-showsource-hide="true" to hide the elementhide: false,// Hide these elements (i.e. the tag) (does not affect to its children). The syntax is as in querySelector; data-showsource-hide-selector="h1,h2,h3,h4,h5,h6,p" to hide the child elements that match these selectorshideSelector: null,// Add to hide list (not overwrite the list) (*) created to be used in the declarative version in the data-showsource-hide-elements-add; has no sense in the programmatic version (not inherited)hideSelectorAdd: null,// Skip these elements (along with its children). The syntax is as in querySelector; data-showsource-skip-selector="h1,h2,h3,h4,h5,h6,p" to skip the element that match these selectorsskipSelector: null,// Add to skipSelector list (not overwrite the list) (*) created to be used in the declarative version in the data-showsource-skip-selector-add; has no sense in the programmatic version (not inherited)skipSelectorAdd: null,// Skip the children of the element, but not the element itself; data-showsource-skip-children="true" or simply data-showsource-skip-children to skip the childrenskipChildren: false,// Hide the attributes related to this plugin; data-showsource-hide-plugin="true" or simply data-showsource-hide-plugin to hide the attributes related to this pluginhidePlugin: true,// The attributes to be removed from the element; data-showsource-remove-attributes="class style" to remove these attributes from the elementremoveAttributes: null,// The class attribute to add to the main container div; data-showsource-class="showsource myclass" to add the classes "showsource" and "myclass" to the main container divclass: "showsource",// Number of characters to break the line of the tag; data-showsource-tag-line-break="80" to break the tag in a new line if the tag is longer than 80 characterstagLineBreak: null,// Max attributes number of consecutive attributes that may appear in a line; data-showsource-max-attributes-per-line="3" to break the attributes in a new line if there are more than 3 attributes in a rowmaxAttributesPerLine: null,// Space separated regular expressions that make that one attribute whose start matches any of them is rendered in a single line (i.e. no other attribute is rendered in the same line); e.g. if the value is data-* then all the attributes starting with data- will be rendered in a single lineseparateElements: null,}

The options can also be used in the declarative way, by adding the data-showsource-* attributes to the element that we want to extract the HTML content and to add the result after it.

The possible options are:

  • data-showsource-indentation is the indentation to be used (the spaces at the beginning of the line), that will be accumulated when rendering the children. The default value is two spaces, so that the source corresponding to the children of the element will have two extra spaces at the beginning. The value is inheritable to the children.
  • data-showsource-hide instructs the library to hide the render of the current element (i.e. the tag) itself, but not its children. The children will be rendered at the level of the current element. The default value is false.
  • data-showsource-hide-selector instructs the library to hide the render of the elements that match the selector (including the current element). The syntax is the same than using document.querySelector. The default value is null.
  • data-showsource-hide-selector-add is a list to be added to the list of selectors to hide. The syntax is the same than using document.querySelector. The default value is null. It has no sense in the programmatic version (not inherited).
  • data-showsource-skip instructs the library to skip the element, along with its children. The default value is false.
  • data-showsource-skip-selector instructs the library to skip the elements that match the selector, along with its children. The syntax is the same than using document.querySelector. The default value is null.
  • data-showsource-skip-selector-add is a list to be added to the list of selectors to skip. The syntax is the same than using document.querySelector. The default value is null. It has no sense in the programmatic version (not inherited).
  • data-showsource-skip-children instruct the library to the children of the element, but not the element itself. The default value is false.
  • data-showsource-hide-plugin instructs the library to hide the attributes related to this plugin (i.e. the data-showsource-* attributes). The default value is true. This attribute is inheritable to the children.
  • data-showsource-remove-attributes is a space separated list of the attributes to be hidden from the element. The default value is null.
  • data-showsource-class is a space separated list of classes to add to the main container div. The default value is showsource.
  • data-showsource-tag-line-break is the number of characters to break the line of the tag. The default value is null (i.e. deactivated). This attribute is inheritable to the children.
  • data-showsource-max-attributes-per-line is the max number of consecutive attributes that may appear in a line. The default value is null (i.e. deactivated). This attribute is inheritable to the children.
  • data-showsource-separate-elements is a space separated list of regular expressions that makes that one attribute that starts matching any of them is rendered in a single line with no other attribute in the same line; e.g. if the value is data-* then all the attributes starting with data- will be rendered in a single line. The default value is null (i.e. deactivated). This attribute is inheritable to the children.

Default options

The default options can be changed by modifying the showsource.defaults object. For example, to change the default indentation to four spaces, we can do the following after including the library:

showsource.defaults.indentation=" ";

To skip the elements that only add text, when using comments while showing how to use a library, we can do the following:

showsource.defaults.skipSelector="h1,h2,h3,h4,h5,h6,p";

About

Generates code snippets from the existing code in a web page. It is ideal to create pages with examples

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Latest commit

History

16 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Extract HTML code from HTML documents (showsource)

This library enables to extract HTML code from the current HTML document, and to add it to the current document in the form of source code.

The library is useful for showing the source code of the current HTML document, and its origin was to ease the creation of web pages for examples of HTML and CSS fragments. My problem was that I wanted to show the example and the code that generated it, but I did not want to maintain two separate code-fragments. This library solves this problem by extracting the HTML code from an element from the current document and adding it to the document in the form of source code.

Use case

The idea is that if we include the following code in the HTML document:

<divclass="example" data-showsource><h2>Simple example</h2><p><spanclass="fw-bold">hint:</span> add the <spanclass="fw-bold">data-showsource</span> attribute to the element that we want to extract the html content and to add the result after it.</p><p>the default behavior is to hide the root element (in this case, <spanclass="fw-bold">data-showsource</span>)</p><divclass="d-flex"><imgsrc="https://picsum.photos/400/200" class="mx-auto"></div></div>

The result will be as shown in the image below: we'll have a block with a title, the texts, and the image. By including the attribute data-showsource, in the envelope (i.e. the examplediv), we'll have the source code of the block after it.

Simple example

Once generated, we can use the library highlightjs to highlight the source code (but this library does not require it).

Usage

The library is available in the form of a CDN, so we can include it in our HTML document as follows:

<scriptsrc="https://cdn.jsdelivr.net/gh/dealfonso/showsource@1/dist/showsource.min.js"></script>

The library will discover any element with the data-showsource attribute and will add the source code of the element after it.

Declarative

The library can be used in a declarative way, by adding the data-showsource attribute to the element that we want to extract the HTML content and to add the result after it. A simple example is shown below:

<divclass="example" data-showsource><h2>Simple example</h2><divclass="d-flex"><imgsrc="https://picsum.photos/400/200" class="mx-auto"></div></div>

Javascript API

It is also possible to use the library from Javascript. So, we can use it to extract the HTML code from an element in a beautiful format (i.e. with indentation and line breaks). Then we can use the result to show it in a dialog, or to add it to the document in a different way.

The library will be available in the global variable showsource with the following methods:

  • showsource.extract(el, userOptions = {}, indent = "") - returns a list of strings with the HTML source code of the element el and its children. The userOptions parameter is optional and allows to customize the behavior of the function. The indent parameter is optional and is used to indent the source code.

  • showsource.show(selector="div[data-showsource]") - discovers any element with the given selector and adds the source code of the element in a div, after it. The function returns the list of elements that have been processed.

    This method is called automatically when the library is loaded, but it can be called again if we add new elements or if we want othe type of elements (e.g. showsource.show("div.example")).

An example of usage is shown below:

// get the element that we want to extract the html contentconstel=document.querySelector(".example");letlines=showsource.extract(el);// add the source code to the documentel.insertAdjacentHTML("afterend",`<pre><code>${lines.join("\n")}</code></pre>`);

Options

The showsource.extract method accepts an optional userOptions parameter that allows to customize the behavior of the function. The following options are available:

{// The indentation to be used (the spaces at the beginning of the line)indentation: " ",// Hide this element (does not affect to its children); data-showsource-hide or data-showsource-hide="true" to hide the elementhide: false,// Hide these elements (i.e. the tag) (does not affect to its children). The syntax is as in querySelector; data-showsource-hide-selector="h1,h2,h3,h4,h5,h6,p" to hide the child elements that match these selectorshideSelector: null,// Add to hide list (not overwrite the list) (*) created to be used in the declarative version in the data-showsource-hide-elements-add; has no sense in the programmatic version (not inherited)hideSelectorAdd: null,// Skip these elements (along with its children). The syntax is as in querySelector; data-showsource-skip-selector="h1,h2,h3,h4,h5,h6,p" to skip the element that match these selectorsskipSelector: null,// Add to skipSelector list (not overwrite the list) (*) created to be used in the declarative version in the data-showsource-skip-selector-add; has no sense in the programmatic version (not inherited)skipSelectorAdd: null,// Skip the children of the element, but not the element itself; data-showsource-skip-children="true" or simply data-showsource-skip-children to skip the childrenskipChildren: false,// Hide the attributes related to this plugin; data-showsource-hide-plugin="true" or simply data-showsource-hide-plugin to hide the attributes related to this pluginhidePlugin: true,// The attributes to be removed from the element; data-showsource-remove-attributes="class style" to remove these attributes from the elementremoveAttributes: null,// The class attribute to add to the main container div; data-showsource-class="showsource myclass" to add the classes "showsource" and "myclass" to the main container divclass: "showsource",// Number of characters to break the line of the tag; data-showsource-tag-line-break="80" to break the tag in a new line if the tag is longer than 80 characterstagLineBreak: null,// Max attributes number of consecutive attributes that may appear in a line; data-showsource-max-attributes-per-line="3" to break the attributes in a new line if there are more than 3 attributes in a rowmaxAttributesPerLine: null,// Space separated regular expressions that make that one attribute whose start matches any of them is rendered in a single line (i.e. no other attribute is rendered in the same line); e.g. if the value is data-* then all the attributes starting with data- will be rendered in a single lineseparateElements: null,}

The options can also be used in the declarative way, by adding the data-showsource-* attributes to the element that we want to extract the HTML content and to add the result after it.

The possible options are:

  • data-showsource-indentation is the indentation to be used (the spaces at the beginning of the line), that will be accumulated when rendering the children. The default value is two spaces, so that the source corresponding to the children of the element will have two extra spaces at the beginning. The value is inheritable to the children.
  • data-showsource-hide instructs the library to hide the render of the current element (i.e. the tag) itself, but not its children. The children will be rendered at the level of the current element. The default value is false.
  • data-showsource-hide-selector instructs the library to hide the render of the elements that match the selector (including the current element). The syntax is the same than using document.querySelector. The default value is null.
  • data-showsource-hide-selector-add is a list to be added to the list of selectors to hide. The syntax is the same than using document.querySelector. The default value is null. It has no sense in the programmatic version (not inherited).
  • data-showsource-skip instructs the library to skip the element, along with its children. The default value is false.
  • data-showsource-skip-selector instructs the library to skip the elements that match the selector, along with its children. The syntax is the same than using document.querySelector. The default value is null.
  • data-showsource-skip-selector-add is a list to be added to the list of selectors to skip. The syntax is the same than using document.querySelector. The default value is null. It has no sense in the programmatic version (not inherited).
  • data-showsource-skip-children instruct the library to the children of the element, but not the element itself. The default value is false.
  • data-showsource-hide-plugin instructs the library to hide the attributes related to this plugin (i.e. the data-showsource-* attributes). The default value is true. This attribute is inheritable to the children.
  • data-showsource-remove-attributes is a space separated list of the attributes to be hidden from the element. The default value is null.
  • data-showsource-class is a space separated list of classes to add to the main container div. The default value is showsource.
  • data-showsource-tag-line-break is the number of characters to break the line of the tag. The default value is null (i.e. deactivated). This attribute is inheritable to the children.
  • data-showsource-max-attributes-per-line is the max number of consecutive attributes that may appear in a line. The default value is null (i.e. deactivated). This attribute is inheritable to the children.
  • data-showsource-separate-elements is a space separated list of regular expressions that makes that one attribute that starts matching any of them is rendered in a single line with no other attribute in the same line; e.g. if the value is data-* then all the attributes starting with data- will be rendered in a single line. The default value is null (i.e. deactivated). This attribute is inheritable to the children.

Default options

The default options can be changed by modifying the showsource.defaults object. For example, to change the default indentation to four spaces, we can do the following after including the library:

showsource.defaults.indentation=" ";

To skip the elements that only add text, when using comments while showing how to use a library, we can do the following:

showsource.defaults.skipSelector="h1,h2,h3,h4,h5,h6,p";

About

Generates code snippets from the existing code in a web page. It is ideal to create pages with examples

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Latest commit

History

16 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Extract HTML code from HTML documents (showsource)

This library enables to extract HTML code from the current HTML document, and to add it to the current document in the form of source code.

The library is useful for showing the source code of the current HTML document, and its origin was to ease the creation of web pages for examples of HTML and CSS fragments. My problem was that I wanted to show the example and the code that generated it, but I did not want to maintain two separate code-fragments. This library solves this problem by extracting the HTML code from an element from the current document and adding it to the document in the form of source code.

Use case

The idea is that if we include the following code in the HTML document:

<divclass="example" data-showsource><h2>Simple example</h2><p><spanclass="fw-bold">hint:</span> add the <spanclass="fw-bold">data-showsource</span> attribute to the element that we want to extract the html content and to add the result after it.</p><p>the default behavior is to hide the root element (in this case, <spanclass="fw-bold">data-showsource</span>)</p><divclass="d-flex"><imgsrc="https://picsum.photos/400/200" class="mx-auto"></div></div>

The result will be as shown in the image below: we'll have a block with a title, the texts, and the image. By including the attribute data-showsource, in the envelope (i.e. the examplediv), we'll have the source code of the block after it.

Simple example

Once generated, we can use the library highlightjs to highlight the source code (but this library does not require it).

Usage

The library is available in the form of a CDN, so we can include it in our HTML document as follows:

<scriptsrc="https://cdn.jsdelivr.net/gh/dealfonso/showsource@1/dist/showsource.min.js"></script>

The library will discover any element with the data-showsource attribute and will add the source code of the element after it.

Declarative

The library can be used in a declarative way, by adding the data-showsource attribute to the element that we want to extract the HTML content and to add the result after it. A simple example is shown below:

<divclass="example" data-showsource><h2>Simple example</h2><divclass="d-flex"><imgsrc="https://picsum.photos/400/200" class="mx-auto"></div></div>

Javascript API

It is also possible to use the library from Javascript. So, we can use it to extract the HTML code from an element in a beautiful format (i.e. with indentation and line breaks). Then we can use the result to show it in a dialog, or to add it to the document in a different way.

The library will be available in the global variable showsource with the following methods:

  • showsource.extract(el, userOptions = {}, indent = "") - returns a list of strings with the HTML source code of the element el and its children. The userOptions parameter is optional and allows to customize the behavior of the function. The indent parameter is optional and is used to indent the source code.

  • showsource.show(selector="div[data-showsource]") - discovers any element with the given selector and adds the source code of the element in a div, after it. The function returns the list of elements that have been processed.

    This method is called automatically when the library is loaded, but it can be called again if we add new elements or if we want othe type of elements (e.g. showsource.show("div.example")).

An example of usage is shown below:

// get the element that we want to extract the html contentconstel=document.querySelector(".example");letlines=showsource.extract(el);// add the source code to the documentel.insertAdjacentHTML("afterend",`<pre><code>${lines.join("\n")}</code></pre>`);

Options

The showsource.extract method accepts an optional userOptions parameter that allows to customize the behavior of the function. The following options are available:

{// The indentation to be used (the spaces at the beginning of the line)indentation: " ",// Hide this element (does not affect to its children); data-showsource-hide or data-showsource-hide="true" to hide the elementhide: false,// Hide these elements (i.e. the tag) (does not affect to its children). The syntax is as in querySelector; data-showsource-hide-selector="h1,h2,h3,h4,h5,h6,p" to hide the child elements that match these selectorshideSelector: null,// Add to hide list (not overwrite the list) (*) created to be used in the declarative version in the data-showsource-hide-elements-add; has no sense in the programmatic version (not inherited)hideSelectorAdd: null,// Skip these elements (along with its children). The syntax is as in querySelector; data-showsource-skip-selector="h1,h2,h3,h4,h5,h6,p" to skip the element that match these selectorsskipSelector: null,// Add to skipSelector list (not overwrite the list) (*) created to be used in the declarative version in the data-showsource-skip-selector-add; has no sense in the programmatic version (not inherited)skipSelectorAdd: null,// Skip the children of the element, but not the element itself; data-showsource-skip-children="true" or simply data-showsource-skip-children to skip the childrenskipChildren: false,// Hide the attributes related to this plugin; data-showsource-hide-plugin="true" or simply data-showsource-hide-plugin to hide the attributes related to this pluginhidePlugin: true,// The attributes to be removed from the element; data-showsource-remove-attributes="class style" to remove these attributes from the elementremoveAttributes: null,// The class attribute to add to the main container div; data-showsource-class="showsource myclass" to add the classes "showsource" and "myclass" to the main container divclass: "showsource",// Number of characters to break the line of the tag; data-showsource-tag-line-break="80" to break the tag in a new line if the tag is longer than 80 characterstagLineBreak: null,// Max attributes number of consecutive attributes that may appear in a line; data-showsource-max-attributes-per-line="3" to break the attributes in a new line if there are more than 3 attributes in a rowmaxAttributesPerLine: null,// Space separated regular expressions that make that one attribute whose start matches any of them is rendered in a single line (i.e. no other attribute is rendered in the same line); e.g. if the value is data-* then all the attributes starting with data- will be rendered in a single lineseparateElements: null,}

The options can also be used in the declarative way, by adding the data-showsource-* attributes to the element that we want to extract the HTML content and to add the result after it.

The possible options are:

  • data-showsource-indentation is the indentation to be used (the spaces at the beginning of the line), that will be accumulated when rendering the children. The default value is two spaces, so that the source corresponding to the children of the element will have two extra spaces at the beginning. The value is inheritable to the children.
  • data-showsource-hide instructs the library to hide the render of the current element (i.e. the tag) itself, but not its children. The children will be rendered at the level of the current element. The default value is false.
  • data-showsource-hide-selector instructs the library to hide the render of the elements that match the selector (including the current element). The syntax is the same than using document.querySelector. The default value is null.
  • data-showsource-hide-selector-add is a list to be added to the list of selectors to hide. The syntax is the same than using document.querySelector. The default value is null. It has no sense in the programmatic version (not inherited).
  • data-showsource-skip instructs the library to skip the element, along with its children. The default value is false.
  • data-showsource-skip-selector instructs the library to skip the elements that match the selector, along with its children. The syntax is the same than using document.querySelector. The default value is null.
  • data-showsource-skip-selector-add is a list to be added to the list of selectors to skip. The syntax is the same than using document.querySelector. The default value is null. It has no sense in the programmatic version (not inherited).
  • data-showsource-skip-children instruct the library to the children of the element, but not the element itself. The default value is false.
  • data-showsource-hide-plugin instructs the library to hide the attributes related to this plugin (i.e. the data-showsource-* attributes). The default value is true. This attribute is inheritable to the children.
  • data-showsource-remove-attributes is a space separated list of the attributes to be hidden from the element. The default value is null.
  • data-showsource-class is a space separated list of classes to add to the main container div. The default value is showsource.
  • data-showsource-tag-line-break is the number of characters to break the line of the tag. The default value is null (i.e. deactivated). This attribute is inheritable to the children.
  • data-showsource-max-attributes-per-line is the max number of consecutive attributes that may appear in a line. The default value is null (i.e. deactivated). This attribute is inheritable to the children.
  • data-showsource-separate-elements is a space separated list of regular expressions that makes that one attribute that starts matching any of them is rendered in a single line with no other attribute in the same line; e.g. if the value is data-* then all the attributes starting with data- will be rendered in a single line. The default value is null (i.e. deactivated). This attribute is inheritable to the children.

Default options

The default options can be changed by modifying the showsource.defaults object. For example, to change the default indentation to four spaces, we can do the following after including the library:

showsource.defaults.indentation=" ";

To skip the elements that only add text, when using comments while showing how to use a library, we can do the following:

showsource.defaults.skipSelector="h1,h2,h3,h4,h5,h6,p";

About

Generates code snippets from the existing code in a web page. It is ideal to create pages with examples

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Latest commit

History

16 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Extract HTML code from HTML documents (showsource)

This library enables to extract HTML code from the current HTML document, and to add it to the current document in the form of source code.

The library is useful for showing the source code of the current HTML document, and its origin was to ease the creation of web pages for examples of HTML and CSS fragments. My problem was that I wanted to show the example and the code that generated it, but I did not want to maintain two separate code-fragments. This library solves this problem by extracting the HTML code from an element from the current document and adding it to the document in the form of source code.

Use case

The idea is that if we include the following code in the HTML document:

<divclass="example" data-showsource><h2>Simple example</h2><p><spanclass="fw-bold">hint:</span> add the <spanclass="fw-bold">data-showsource</span> attribute to the element that we want to extract the html content and to add the result after it.</p><p>the default behavior is to hide the root element (in this case, <spanclass="fw-bold">data-showsource</span>)</p><divclass="d-flex"><imgsrc="https://picsum.photos/400/200" class="mx-auto"></div></div>

The result will be as shown in the image below: we'll have a block with a title, the texts, and the image. By including the attribute data-showsource, in the envelope (i.e. the examplediv), we'll have the source code of the block after it.

Simple example

Once generated, we can use the library highlightjs to highlight the source code (but this library does not require it).

Usage

The library is available in the form of a CDN, so we can include it in our HTML document as follows:

<scriptsrc="https://cdn.jsdelivr.net/gh/dealfonso/showsource@1/dist/showsource.min.js"></script>

The library will discover any element with the data-showsource attribute and will add the source code of the element after it.

Declarative

The library can be used in a declarative way, by adding the data-showsource attribute to the element that we want to extract the HTML content and to add the result after it. A simple example is shown below:

<divclass="example" data-showsource><h2>Simple example</h2><divclass="d-flex"><imgsrc="https://picsum.photos/400/200" class="mx-auto"></div></div>

Javascript API

It is also possible to use the library from Javascript. So, we can use it to extract the HTML code from an element in a beautiful format (i.e. with indentation and line breaks). Then we can use the result to show it in a dialog, or to add it to the document in a different way.

The library will be available in the global variable showsource with the following methods:

  • showsource.extract(el, userOptions = {}, indent = "") - returns a list of strings with the HTML source code of the element el and its children. The userOptions parameter is optional and allows to customize the behavior of the function. The indent parameter is optional and is used to indent the source code.

  • showsource.show(selector="div[data-showsource]") - discovers any element with the given selector and adds the source code of the element in a div, after it. The function returns the list of elements that have been processed.

    This method is called automatically when the library is loaded, but it can be called again if we add new elements or if we want othe type of elements (e.g. showsource.show("div.example")).

An example of usage is shown below:

// get the element that we want to extract the html contentconstel=document.querySelector(".example");letlines=showsource.extract(el);// add the source code to the documentel.insertAdjacentHTML("afterend",`<pre><code>${lines.join("\n")}</code></pre>`);

Options

The showsource.extract method accepts an optional userOptions parameter that allows to customize the behavior of the function. The following options are available:

{// The indentation to be used (the spaces at the beginning of the line)indentation: " ",// Hide this element (does not affect to its children); data-showsource-hide or data-showsource-hide="true" to hide the elementhide: false,// Hide these elements (i.e. the tag) (does not affect to its children). The syntax is as in querySelector; data-showsource-hide-selector="h1,h2,h3,h4,h5,h6,p" to hide the child elements that match these selectorshideSelector: null,// Add to hide list (not overwrite the list) (*) created to be used in the declarative version in the data-showsource-hide-elements-add; has no sense in the programmatic version (not inherited)hideSelectorAdd: null,// Skip these elements (along with its children). The syntax is as in querySelector; data-showsource-skip-selector="h1,h2,h3,h4,h5,h6,p" to skip the element that match these selectorsskipSelector: null,// Add to skipSelector list (not overwrite the list) (*) created to be used in the declarative version in the data-showsource-skip-selector-add; has no sense in the programmatic version (not inherited)skipSelectorAdd: null,// Skip the children of the element, but not the element itself; data-showsource-skip-children="true" or simply data-showsource-skip-children to skip the childrenskipChildren: false,// Hide the attributes related to this plugin; data-showsource-hide-plugin="true" or simply data-showsource-hide-plugin to hide the attributes related to this pluginhidePlugin: true,// The attributes to be removed from the element; data-showsource-remove-attributes="class style" to remove these attributes from the elementremoveAttributes: null,// The class attribute to add to the main container div; data-showsource-class="showsource myclass" to add the classes "showsource" and "myclass" to the main container divclass: "showsource",// Number of characters to break the line of the tag; data-showsource-tag-line-break="80" to break the tag in a new line if the tag is longer than 80 characterstagLineBreak: null,// Max attributes number of consecutive attributes that may appear in a line; data-showsource-max-attributes-per-line="3" to break the attributes in a new line if there are more than 3 attributes in a rowmaxAttributesPerLine: null,// Space separated regular expressions that make that one attribute whose start matches any of them is rendered in a single line (i.e. no other attribute is rendered in the same line); e.g. if the value is data-* then all the attributes starting with data- will be rendered in a single lineseparateElements: null,}

The options can also be used in the declarative way, by adding the data-showsource-* attributes to the element that we want to extract the HTML content and to add the result after it.

The possible options are:

  • data-showsource-indentation is the indentation to be used (the spaces at the beginning of the line), that will be accumulated when rendering the children. The default value is two spaces, so that the source corresponding to the children of the element will have two extra spaces at the beginning. The value is inheritable to the children.
  • data-showsource-hide instructs the library to hide the render of the current element (i.e. the tag) itself, but not its children. The children will be rendered at the level of the current element. The default value is false.
  • data-showsource-hide-selector instructs the library to hide the render of the elements that match the selector (including the current element). The syntax is the same than using document.querySelector. The default value is null.
  • data-showsource-hide-selector-add is a list to be added to the list of selectors to hide. The syntax is the same than using document.querySelector. The default value is null. It has no sense in the programmatic version (not inherited).
  • data-showsource-skip instructs the library to skip the element, along with its children. The default value is false.
  • data-showsource-skip-selector instructs the library to skip the elements that match the selector, along with its children. The syntax is the same than using document.querySelector. The default value is null.
  • data-showsource-skip-selector-add is a list to be added to the list of selectors to skip. The syntax is the same than using document.querySelector. The default value is null. It has no sense in the programmatic version (not inherited).
  • data-showsource-skip-children instruct the library to the children of the element, but not the element itself. The default value is false.
  • data-showsource-hide-plugin instructs the library to hide the attributes related to this plugin (i.e. the data-showsource-* attributes). The default value is true. This attribute is inheritable to the children.
  • data-showsource-remove-attributes is a space separated list of the attributes to be hidden from the element. The default value is null.
  • data-showsource-class is a space separated list of classes to add to the main container div. The default value is showsource.
  • data-showsource-tag-line-break is the number of characters to break the line of the tag. The default value is null (i.e. deactivated). This attribute is inheritable to the children.
  • data-showsource-max-attributes-per-line is the max number of consecutive attributes that may appear in a line. The default value is null (i.e. deactivated). This attribute is inheritable to the children.
  • data-showsource-separate-elements is a space separated list of regular expressions that makes that one attribute that starts matching any of them is rendered in a single line with no other attribute in the same line; e.g. if the value is data-* then all the attributes starting with data- will be rendered in a single line. The default value is null (i.e. deactivated). This attribute is inheritable to the children.

Default options

The default options can be changed by modifying the showsource.defaults object. For example, to change the default indentation to four spaces, we can do the following after including the library:

showsource.defaults.indentation=" ";

To skip the elements that only add text, when using comments while showing how to use a library, we can do the following:

showsource.defaults.skipSelector="h1,h2,h3,h4,h5,h6,p";

About

Generates code snippets from the existing code in a web page. It is ideal to create pages with examples

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages