Skip to content

Commit f1c39e9

Browse files
committed
improve comment block printing
1 parent ff4ea00 commit f1c39e9

File tree

6 files changed

+68
-40
lines changed

6 files changed

+68
-40
lines changed

dist/twcss-to-sass.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.html

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,17 @@
4343
TailwindCSS to SASS Converter
4444
</v-toolbar-title>
4545
</v-col>
46-
<v-col cols="2" class="d-flex justify-end"></v-col>
46+
<v-col cols="2" class="d-flex justify-end">
47+
<a class="github-button mx-10"
48+
href="https://github.com/EgoistDeveloper/twcss-to-sass"
49+
data-color-scheme="no-preference: dark; light: light; dark: dark;"
50+
data-icon="octicon-star"
51+
data-size="large"
52+
data-show-count="true"
53+
aria-label="Star facebook/react on GitHub">
54+
Star
55+
</a>
56+
</v-col>
4757
</v-toolbar>
4858

4959
<v-main class="my-5 mx-5">
@@ -103,6 +113,9 @@
103113
<script src="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.26.1/min/vs/editor/editor.main.nls.js"></script>
104114
<script src="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.26.1/min/vs/editor/editor.main.js"></script>
105115

116+
<!-- Github Buttons -->
117+
<script async defer src="https://buttons.github.io/buttons.js"></script>
118+
106119
<!-- App -->
107120
<script>
108121
const isBrowserDarkTheme = window.matchMedia('(prefers-color-scheme: dark)').matches;
@@ -126,7 +139,7 @@
126139
/** load monaco code editors */
127140
loadEditors: function () {
128141
const defaultTemplate =
129-
`<div class="bg-white">\n\t<div class="flex justify-center items-center min-h-screen min-w-full">\n\t\t<div class="flex relative">\n\t\t\t<div class="w-72 h-40 bg-green-400 transform transition-all skew-x-12 -skew-y-12 absolute rounded-lg">\n\t\t\t\tMy Text 1\n\t\t\t</div>\n\t\t</div>\n\t</div>\n</div>`;
142+
`<!-- Container Start -->\n<!-- Container Any -->\n<div class="bg-white">\n\t<!-- Some Div -->\n\t<div class="flex justify-center items-center min-h-screen min-w-full">\n\t\t<div class="flex relative">\n\t\t\t<!-- Inner Div -->\n\t\t\t<div class="w-72 h-40 bg-green-400 transform transition-all skew-x-12 -skew-y-12 absolute rounded-lg">\n\t\t\t\tMy Text 1\n\t\t\t</div>\n\t\t</div>\n\t</div>\n</div>\n<!-- Container End-->`;
130143

131144
const {
132145
convertToSass

examples/browser/twcss-to-sass.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/node/twcss-to-sass.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@egoistdeveloper/twcss-to-sass",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "HTML template to SASS converter for TailwindCSS",
55
"source": "src/index.js",
66
"main": "dist/twcss-to-sass.js",

src/index.js

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -93,36 +93,55 @@ const getStyleContents = function (styleElements) {
9393
*
9494
* @returns Object
9595
*/
96-
const filterHtmlData = function (htmlJson) {
96+
const filterHtmlData = function (htmlJson, nestedOrder = 1) {
9797
if (htmlJson && Array.isArray(htmlJson)) {
98-
var _data = htmlJson.filter((x) => x.type == 'element' && x.tagName != 'style'),
98+
var parentNode = htmlJson.filter((x) => (x.type == 'element' || x.type == 'comment') && x.tagName != 'style'),
9999
styleElements = htmlJson.filter((x) => x.tagName == 'style'),
100100
styleList = [];
101101

102102
if (styleElements && styleElements.length) {
103103
styleList = getStyleContents(styleElements);
104104
}
105105

106-
var elementList = _data.map((node) => {
106+
var elementList = parentNode.map((node) => {
107107
if (Array.isArray(node.children)) {
108-
let children = filterHtmlData(node.children);
108+
var previousNodes = [];
109+
110+
// find available previous nodes
111+
for (let i = 0; i < parentNode.length; i++) {
112+
if (parentNode[i] == node){
113+
if (parentNode[i - 1]){
114+
previousNodes.push(parentNode[i - 1]);
115+
}
116+
117+
if (parentNode[i - 2]){
118+
previousNodes.push(parentNode[i - 2]);
119+
}
120+
121+
break;
122+
}
123+
}
109124

110-
// find text nodes and merge
111-
node.text = children.filter((x) => x.type == 'text').map((x) => {
112-
return utils.cleanText(x.content, true);
113-
}).filter((x) => x !== null).join(' ');
125+
// get parent comment text
126+
node.comment = previousNodes.filter((x) => x.type == 'comment')
127+
.map((x) => utils.cleanText(x.content, true))
128+
.filter((x) => x !== null)
129+
.reverse()
130+
.join(', ');
131+
132+
node.comment = node.comment ? node.comment : node.tagName;
133+
node.order = nestedOrder;
114134

115-
// last cleanup
116-
node.text = utils.cleanText(node.text, true);
135+
let children = filterHtmlData(node.children, nestedOrder + 1);
117136

118-
// get elements
137+
// get only html elements
119138
node.children = children.length ? children.filter((x) => x.type == 'element') : null;
120139
}
121140

122-
// get specific attributes
141+
// get only class and inline style attributes
123142
node.attributes = getAttributes(node.attributes, ['class', 'style']);
124143

125-
return node.attributes !== null || node.children !== null ? node : null;
144+
return (node.attributes !== null || node.children !== null) ? node : null;
126145
});
127146

128147
elementList = elementList.filter((node) => node !== null && node !== undefined);
@@ -141,53 +160,48 @@ const filterHtmlData = function (htmlJson) {
141160
*
142161
* @returns string
143162
*/
144-
const getSassTree = function (nodeTree, count = 0) {
163+
const getSassTree = function (nodeTree, deepth = 0) {
145164
if (nodeTree) {
146-
var _data = null,
147-
styleCount = 0;
165+
var styleCount = 0;
148166

149-
if (Array.isArray(nodeTree)) {
150-
_data = nodeTree;
151-
} else if (Array.isArray(nodeTree.children)) {
152-
_data = nodeTree.children;
167+
if (!Array.isArray(nodeTree)) {
168+
nodeTree = nodeTree.children;
153169
}
154170

155-
return _data.map((node) => {
156-
let block = '',
157-
blocks = '';
171+
return nodeTree.map((node) => {
172+
let treeSTring = '',
173+
subTreeSTring = '';
158174

159175
if (node.attributes === null && node.children === null) {
160176
return '';
161177
}
162178

163179
if (Array.isArray(node.children) && node.children.length) {
164-
++count;
180+
++deepth;
165181

166-
blocks = getSassTree(node, count);
182+
subTreeSTring = getSassTree(node, deepth);
167183
}
168184

169185
if (node.tagName == 'style' && node.attributes) {
170186
styleCount += 1;
171187

172-
var result = `// #region STYLE #${styleCount}\n`;
173-
188+
let result = `// #region STYLE #${styleCount}\n`;
174189
result += `\n${node.attributes.style}\n`;
175190
result += '// #endregion\n\n';
176191

177192
return result;
178193
} else {
179194
if (node.attributes) {
180-
block += node.attributes.class ? `@apply ${node.attributes.class};` : '';
195+
treeSTring += node.attributes.class ? `@apply ${node.attributes.class};` : '';
181196

182197
node.attributes.style = utils.addMissingSuffix(node.attributes.style, ';');
183-
block += node.attributes.style ? `\n${node.attributes.style}\n` : '';
198+
treeSTring += node.attributes.style ? `\n${node.attributes.style}\n` : '';
184199
}
185200

186-
if (block.length || blocks.length) {
187-
let result = `/* ${node.tagName} -> ${node.text ? node.text : 'NOTEXT'} */`;
188-
189-
result += `.any-${count}-${node.tagName}`;
190-
result += `{${block}/*#block_${node.children ? node.children.length : '-'}*/${blocks}}`;
201+
if (treeSTring.length || subTreeSTring.length) {
202+
let result = `/* ${node.comment} -> ${node.order} */`;
203+
result += `.class-${deepth}-${node.tagName}`;
204+
result += `{${treeSTring}${subTreeSTring}}`;
191205

192206
return result;
193207
}
@@ -216,6 +230,7 @@ module.exports = {
216230
filteredHtmlData = filterHtmlData(htmlJson),
217231
sassTreeResult = getSassTree(filteredHtmlData);
218232

233+
// export with formatted output
219234
if (options && options.formatOutput === true) {
220235
var _formatterOptions = {};
221236

0 commit comments

Comments
 (0)