Skip to content

Commit c0fe71c

Browse files
authored
Merge pull request #2 from dealfonso/remove-remove
remove the _remove_ option and add the hide selector and skip selector
2 parents 151782e + 377ebd7 commit c0fe71c

File tree

5 files changed

+162
-68
lines changed

5 files changed

+162
-68
lines changed

README.md

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,39 +76,46 @@ The `showsource.extract` method accepts an optional `userOptions` parameter that
7676

7777
```javascript
7878
{
79-
// The indentation to be used (the spaces at the beginning of the line) (inheritable)
79+
// The indentation to be used (the spaces at the beginning of the line)
8080
indentation: " ",
81-
// Remove the children of these types of elements
82-
remove: "h1 h2 h3 h4 h5 h6 p",
83-
// Hide the element (i.e. the tag) itself, but not its children
81+
// Hide this element (does not affect to its children); data-showsource-hide or data-showsource-hide="true" to hide the element
8482
hide: false,
85-
// Skip the element, along with its children
86-
skip: false,
87-
// Skip the children of the element, but not the element itself
83+
// 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 selectors
84+
hideSelector: null,
85+
// 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)
86+
hideSelectorAdd: null,
87+
// 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 selectors
88+
skipSelector: null,
89+
// 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)
90+
skipSelectorAdd: null,
91+
// 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 children
8892
skipChildren: false,
89-
// Hide the attributes related to this plugin (inheritable)
93+
// 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 plugin
9094
hidePlugin: true,
91-
// The attributes to be removed from the element
95+
// The attributes to be removed from the element; data-showsource-remove-attributes="class style" to remove these attributes from the element
9296
removeAttributes: null,
93-
// The class attribute to add to the main container div
97+
// 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 div
9498
class: "showsource",
95-
// Number of characters to break the line of the tag (inheritable)
99+
// 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 characters
96100
tagLineBreak: null,
97-
// Max attributes number of consecutive attributes that may appear in a line (inheritable)
101+
// 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 row
98102
maxAttributesPerLine: null,
99-
// 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 line (inheritable)
100-
separateElements: null,
103+
// 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 line
104+
separateElements: null,
101105
}
102106
```
103107

104108
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.
105109

106110
The possible options are:
107111
- `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.
108-
- `data-showsource-remove` is a space separated list of html elements (or selectors) to be removed from the element. The default value is `h1 h2 h3 h4 h5 h6 p`.
109112
- `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`.
113+
- `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`.
114+
- `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).
110115
- `data-showsource-skip` instructs the library to skip the element, along with its children. The default value is `false`.
111-
- `data-showsource-skip-children` instruct the library to the children of the element, but not the element itself (unless the `data-showsource-hide` attribute is also present). The default value is `false`.
116+
- `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`.
117+
- `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).
118+
- `data-showsource-skip-children` instruct the library to the children of the element, but not the element itself. The default value is `false`.
112119
- `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.
113120
- `data-showsource-remove-attributes` is a space separated list of the attributes to be hidden from the element. The default value is `null`.
114121
- `data-showsource-class` is a space separated list of classes to add to the main container div. The default value is `showsource`.
@@ -122,4 +129,10 @@ The default options can be changed by modifying the `showsource.defaults` object
122129

123130
```javascript
124131
showsource.defaults.indentation = " ";
132+
```
133+
134+
To skip the elements that only add text, when using comments while showing how to use a library, we can do the following:
135+
136+
```javascript
137+
showsource.defaults.skipSelector = "h1,h2,h3,h4,h5,h6,p";
125138
```

dist/showsource.js

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
2626
"use strict";
2727
let defaultOptions = {
2828
indentation: " ",
29-
remove: "h1 h2 h3 h4 h5 h6 p",
3029
hide: false,
30+
hideSelector: null,
31+
hideSelectorAdd: null,
3132
skip: false,
33+
skipSelector: null,
34+
skipSelectorAdd: null,
3235
skipChildren: false,
3336
hidePlugin: true,
3437
removeAttributes: null,
@@ -37,17 +40,26 @@
3740
maxAttributesPerLine: null,
3841
separateElements: null
3942
};
40-
function beautify(el, userOptions = {}, indent = "") {
43+
function extractCode(el, userOptions = {}, indent = "") {
4144
let elOptions = {};
4245
if (el.dataset.showsourceIndentation !== undefined) {
4346
elOptions.indentation = el.dataset.showsourceIndentation;
4447
}
45-
if (el.dataset.showsourceRemove !== undefined) {
46-
elOptions.remove = el.dataset.showsourceRemove;
48+
if (el.dataset.showsourceSkipSelector !== undefined) {
49+
elOptions.skipSelector = el.dataset.showsourceSkipSelector;
50+
}
51+
if (el.dataset.showsourceSkipSelectorAdd !== undefined) {
52+
elOptions.skipSelectorAdd = el.dataset.showsourceSkipSelectorAdd;
4753
}
4854
if (el.dataset.showsourceHide !== undefined) {
4955
elOptions.hide = el.dataset.showsourceHide.toLowerCase() != "false";
5056
}
57+
if (el.dataset.showsourceHideSelector !== undefined) {
58+
elOptions.hideSelector = el.dataset.showsourceHideSelector;
59+
}
60+
if (el.dataset.showsourceHideSelectorAdd !== undefined) {
61+
elOptions.hideSelectorAdd = el.dataset.showsourceHideSelectorAdd;
62+
}
5163
if (el.dataset.showsourceSkip !== undefined) {
5264
elOptions.skip = el.dataset.showsourceSkip.toLowerCase() != "false";
5365
}
@@ -70,23 +82,46 @@
7082
if (options.skip) {
7183
return [];
7284
}
73-
el = el.cloneNode(true);
85+
if (typeof options.skipSelector === "string") {
86+
let elementsSkip = options.skipSelector.split(",").filter(e => e.trim() !== "").join(",");
87+
if (elementsSkip.trim() != "") {
88+
if (el.matches(elementsSkip)) {
89+
return [];
90+
}
91+
}
92+
}
93+
if (typeof options.skipSelectorAdd === "string") {
94+
if (options.skipSelector === undefined || options.skipSelector === null) {
95+
options.skipSelector = options.skipSelectorAdd;
96+
} else {
97+
options.skipSelector += ", " + options.skipSelectorAdd;
98+
}
99+
}
100+
if (typeof options.hideSelectorAdd === "string") {
101+
if (options.hideSelector === undefined || options.hideSelector === null) {
102+
options.hideSelector = options.hideSelectorAdd;
103+
} else {
104+
options.hideSelector += "," + options.hideSelectorAdd;
105+
}
106+
}
74107
let childOptions = Object.assign({}, userOptions, {
75108
indentation: options.indentation,
76109
hidePlugin: options.hidePlugin,
77110
tagLineBreak: options.tagLineBreak,
78111
maxAttributesPerLine: options.maxAttributesPerLine,
79-
separateElements: options.separateElements
112+
separateElements: options.separateElements,
113+
skipSelector: options.skipSelector,
114+
hideSelector: options.hideSelector
80115
});
81-
if (typeof options.remove === "string") {
82-
let elementsRemove = options.remove.split(" ").join(",");
83-
if (elementsRemove.trim() != "") {
84-
el.querySelectorAll(elementsRemove).forEach(function(e) {
85-
e.remove();
86-
});
116+
let beautifulElement = [];
117+
if (!options.hide) {
118+
if (typeof options.hideSelector === "string") {
119+
let elementsHide = options.hideSelector.split(",").filter(x => x.trim() !== "").join(",");
120+
if (elementsHide.trim() != "") {
121+
options.hide = el.matches(elementsHide);
122+
}
87123
}
88124
}
89-
let beautifulElement = [];
90125
if (!options.hide) {
91126
beautifulElement.push(indent + "<" + el.tagName.toLowerCase());
92127
let removeAttributes = [];
@@ -164,7 +199,7 @@
164199
lines.pop();
165200
}
166201
beautifulElement.push(lines.join("\n"));
167-
beautifulElement.push("</" + el.tagName.toLowerCase() + ">");
202+
beautifulElement.push(indent + "</" + el.tagName.toLowerCase() + ">");
168203
} else {
169204
if (beautifulElement.length > 1) {
170205
beautifulElement[beautifulElement.length - 1] += ">" + el.innerHTML.trim();
@@ -186,7 +221,7 @@
186221
beautifulElement.push(indent + options.indentation + text);
187222
}
188223
} else {
189-
let beautifulChild = beautify(child, childOptions, indent + (options.hide ? "" : options.indentation));
224+
let beautifulChild = extractCode(child, childOptions, indent + (options.hide ? "" : options.indentation));
190225
beautifulElement = beautifulElement.concat(beautifulChild);
191226
}
192227
}
@@ -195,7 +230,6 @@
195230
beautifulElement.push(indent + "</" + el.tagName.toLowerCase() + ">");
196231
}
197232
}
198-
el.remove();
199233
return beautifulElement;
200234
}
201235
function init() {
@@ -211,7 +245,7 @@
211245
let container = document.createElement("div");
212246
let pre = document.createElement("pre");
213247
let code = document.createElement("code");
214-
code.textContent = beautify(el).join("\n");
248+
code.textContent = extractCode(el).join("\n");
215249
pre.appendChild(code);
216250
container.appendChild(pre);
217251
classString.split(" ").forEach(function(c) {
@@ -227,8 +261,8 @@
227261
}
228262
window.showsource = {
229263
defaults: Object.assign({}, defaultOptions),
230-
beautify: beautify,
264+
extract: extractCode,
231265
init: init,
232-
version: "1.1.1"
266+
version: "1.2.0"
233267
};
234268
})(window, document);

0 commit comments

Comments
 (0)