Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix transform source bug #1918

Merged
merged 5 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
rajsite marked this conversation as resolved.
Show resolved Hide resolved
"type": "none",
"comment": "Fix Storybook source preview",
"packageName": "@ni/nimble-components",
"email": "1458528+fredvisser@users.noreply.github.com",
"dependentChangeType": "none"
}
135 changes: 134 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 8 additions & 10 deletions packages/nimble-components/.storybook/transformSource.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import prettier from 'prettier/standalone';
import parserHTML from 'prettier/plugins/html';
const beautifyHTML = require('js-beautify').html;

rajsite marked this conversation as resolved.
Show resolved Hide resolved
const createFragmentFromHTML = html => {
const template = document.createElement('template');
Expand Down Expand Up @@ -51,7 +50,10 @@ const removeClassAttributes = node => {
}
// Assume that all class attributes added to nimble elements were added by FAST
// and are not part of the control api
if (node instanceof HTMLElement && node.tagName.toLowerCase().startsWith('nimble-')) {
if (
node instanceof HTMLElement
&& node.tagName.toLowerCase().startsWith('nimble-')
) {
node.removeAttribute('class');
}
};
Expand All @@ -61,8 +63,7 @@ const removeBlankLines = html => html
.filter(line => line.trim() !== '')
.join('\n');

const removeEmptyAttributes = html => html
.replaceAll('=""', '');
const removeEmptyAttributes = html => html.replaceAll('=""', '');

// A custom source transformer. See:
// https://github.com/storybookjs/storybook/blob/next/addons/docs/docs/recipes.md#customizing-source-snippets
Expand All @@ -78,11 +79,8 @@ export const transformSource = source => {

const trimmedHTML = removeBlankLines(html);
const emptyAttributesRemovedHTML = removeEmptyAttributes(trimmedHTML);
const formmattedHTML = prettier.format(emptyAttributesRemovedHTML, {
parser: 'html',
plugins: [parserHTML],
htmlWhitespaceSensitivity: 'ignore',
tabWidth: 4
const formmattedHTML = beautifyHTML(emptyAttributesRemovedHTML, {
wrap_attributes: 'force-expand-multiline'
});
rajsite marked this conversation as resolved.
Show resolved Hide resolved
return formmattedHTML;
};
2 changes: 1 addition & 1 deletion packages/nimble-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
"eslint-plugin-storybook": "^0.8.0",
"html-webpack-plugin": "^5.3.1",
"jasmine-core": "^5.1.2",
"js-beautify": "^1.15.1",
"karma": "^6.3.0",
rajsite marked this conversation as resolved.
Show resolved Hide resolved
"karma-chrome-launcher": "^3.1.0",
"karma-firefox-launcher": "^2.1.0",
Expand All @@ -151,7 +152,6 @@
"karma-webkit-launcher": "^2.1.0",
"karma-webpack": "^5.0.0",
"playwright": "1.40.0",
"prettier": "^3.2.5",
"prettier-eslint": "^16.3.0",
"prettier-eslint-cli": "^8.0.1",
"remark-gfm": "^3.0.1",
Expand Down