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

Update to docusaurus 2 #193

Merged
merged 6 commits into from Jun 1, 2021
Merged

Update to docusaurus 2 #193

merged 6 commits into from Jun 1, 2021

Conversation

jeddy3
Copy link
Member

@jeddy3 jeddy3 commented May 30, 2021

Which issue, if any, is this issue related to?

Closes #125
Supercedes #174

Is there anything in the PR that needs further explanation?

Rebasing the other branch was a PITA as it was a few months old.

This pull requests takes @m-allanson work and layers it on the latest code.

@jeddy3
Copy link
Member Author

jeddy3 commented May 30, 2021

Deploy preview is at:
https://deploy-preview-193--stylelint.netlify.app/

I believe the only thing not working is the validity colours for the examples on each rule page. I think we can deploy without it as it's a nice-to-have enhancement. Then, when someone has time, they can look into the proper docusaurus 2 way of achieving that behaviour.

patches/stylelint+13.13.1.patch Outdated Show resolved Hide resolved
package.json Show resolved Hide resolved
netlify.toml Show resolved Hide resolved
@ybiquitous
Copy link
Member

I believe the only thing not working is the validity colours for the examples on each rule page. I think we can deploy without it as it's a nice-to-have enhancement. Then, when someone has time, they can look into the proper docusaurus 2 way of achieving that behaviour.

The current script pattern-validity.js depends on DOM manipulation, but it seems difficult to take the same way due to the Docusaurus v2 rendering system change.

document.addEventListener('DOMContentLoaded', () => {

So, I suggest a different way to process Markdown files, instead of DOM manipulation. For example, the following way just adds emojis, not changing the background color via CSS:

diff --git a/scripts/generate-docs.mjs b/scripts/generate-docs.mjs
index c72d134..0114430 100644
--- a/scripts/generate-docs.mjs
+++ b/scripts/generate-docs.mjs
@@ -4,21 +4,46 @@ import * as path from 'path';
 import { default as remark } from 'remark';
 import { visit } from 'unist-util-visit';
 
+function rewriteLink(options) {
+	function visitor(node) {
+		node.url = options.rewriter(node.url);
+	}
+
+	function transform(tree) {
+		visit(tree, ['link'], visitor);
+	}
+
+	return transform;
+}
+
+function addSymbolToViolationExample() {
+	function visitor(node) {
+		const children = node.children;
+
+		if (
+			children[0].value?.startsWith('The following pattern') &&
+			children[children.length - 1].value?.includes('considered violation')
+		) {
+			const isNotViolation = children[1]?.type === 'emphasis';
+
+			children.unshift({
+				type: 'text',
+				value: isNotViolation ? '👍 ' : '👎 ',
+			});
+		}
+	}
+
+	function transform(tree) {
+		visit(tree, ['paragraph'], visitor);
+	}
+
+	return transform;
+}
+
 function processMarkdown(file, { rewriter }) {
-	function rewriteLink(options) {
-		function visitor(node) {
-			node.url = options.rewriter(node.url);
-		}
-
-		function transform(tree) {
-			visit(tree, ['link'], visitor);
-		}
-
-		return transform;
-	}
-
 	const content = remark()
 		.use(rewriteLink, { rewriter })
+		.use(addSymbolToViolationExample)
 		.processSync(fs.readFileSync(file, 'utf8'))
 		.toString();
 

Result:

image

@jeddy3
Copy link
Member Author

jeddy3 commented May 31, 2021

@ybiquitous Using remark to modify the markdown is a much cleaner solution! Would it be difficult to wrap the code fences that follow one of the trigger paragraphs in a <div> with the appropriate class?

For example:

Given:

    ["opacity"]

The following patterns are considered violations:

<div class="invalid-pattern">

```css
a { opacity: 5ff0% }
```

</div>

<div class="invalid-pattern">

```css
a { color: rgb(0 0 0 / 0.5) }
```

</div>

The following patterns are *not* considered violations:

<div class="valid-pattern">

```css
a { opacity: 0.5 }
```

</div>

<div class="valid-pattern">

```css
a { color: rgb(0 0 0 / 50%) }
```

</div>

A similar transform is used here.

The logic would be:

  • for each trigger paragraph
  • wrap the the following code fences in a div (with the appropriate class)
  • until a heading or another trigger paragraph is reached

I think that'll cover all cases we have in our rule READMEs.

We'd then be able to do:

Screenshot 2021-05-31 at 09 08 39

Which is less noisy and easier to scan than emojis, especially when a rule has many options.

@ybiquitous
Copy link
Member

@jeddy3 Looks good! I will look into the way. 💪🏼

@jeddy3
Copy link
Member Author

jeddy3 commented May 31, 2021

FYI, the styling in the screenshot above was achieved by updating custom.css to:

.valid-pattern .prism-code {
  box-shadow: 0 -2px 0 0 green;
}

.invalid-pattern .prism-code {
  box-shadow: 0 -2px 0 0 crimson;
}

@ybiquitous
Copy link
Member

@jeddy3 Thank you for the advice. Perhaps, the following code should produce what you expect:

diff --git a/scripts/generate-docs.mjs b/scripts/generate-docs.mjs
index c72d134..322e66e 100644
--- a/scripts/generate-docs.mjs
+++ b/scripts/generate-docs.mjs
@@ -4,21 +4,75 @@ import * as path from 'path';
 import { default as remark } from 'remark';
 import { visit } from 'unist-util-visit';
 
+function rewriteLink(options) {
+	function visitor(node) {
+		node.url = options.rewriter(node.url);
+	}
+
+	function transform(tree) {
+		visit(tree, ['link'], visitor);
+	}
+
+	return transform;
+}
+
+function wrapViolationExample() {
+	function visitor(node, index, parent) {
+		const isInvalid = node.children[1]?.children[0]?.value === 'not';
+		const className = isInvalid ? 'invalid-pattern' : 'valid-pattern';
+		const wrapperStart = { type: 'html', value: `<div class="${className}">` };
+		const wrapperEnd = { type: 'html', value: '</div>' };
+
+		// Insert to the next position of the paragraph
+		parent.children.splice(index + 1, 0, wrapperStart);
+
+		const childrenLength = parent.children.length;
+
+		// Find the end position
+		let endIndex = index + 1;
+
+		while (endIndex < childrenLength) {
+			const child = parent.children[endIndex];
+
+			if (child.type === 'paragraph' || child.type === 'heading') {
+				break;
+			}
+
+			endIndex++;
+		}
+
+		// Insert to the end position
+		if (endIndex === childrenLength) {
+			parent.children.push(wrapperEnd);
+		} else {
+			parent.children.splice(endIndex, 0, wrapperEnd);
+		}
+	}
+
+	function test(node) {
+		if (!node.children) return false;
+
+		// There can be an `emphasis` node with "not" between the `first` and `last` nodes
+		const [first, , last] = node.children;
+		const text = (first?.value ?? '').trimEnd() + (last?.value ?? '');
+
+		return [
+			'The following patterns are considered violations:',
+			'The following pattern is considered a violation:',
+		].includes(text);
+	}
+
+	function transform(tree) {
+		visit(tree, test, visitor);
+	}
+
+	return transform;
+}
+
 function processMarkdown(file, { rewriter }) {
-	function rewriteLink(options) {
-		function visitor(node) {
-			node.url = options.rewriter(node.url);
-		}
-
-		function transform(tree) {
-			visit(tree, ['link'], visitor);
-		}
-
-		return transform;
-	}
-
 	const content = remark()
 		.use(rewriteLink, { rewriter })
+		.use(wrapViolationExample)
 		.processSync(fs.readFileSync(file, 'utf8'))
 		.toString();
 

package.json Outdated Show resolved Hide resolved
src/pages/demo.js Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
@jeddy3
Copy link
Member Author

jeddy3 commented Jun 1, 2021

Perhaps, the following code should produce what you expect:

Many thanks! Looks great. I tweaked it a little to close on either a heading or a trigger paragraph, rather than any paragraph, as sometimes we have paragraphs within validity blocks, e.g. old color-no-hex page & new color-no-hex page

Funnily enough, we used to use remark to do the validity stuff 4 years ago when the website was built using Phenomic. Kinda odd that we're back to using React again all these year later.

@jeddy3
Copy link
Member Author

jeddy3 commented Jun 1, 2021

I think that's everything done now. Whatcha think, shall we push it live?

Copy link
Member

@ybiquitous ybiquitous left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeddy3 This is great work! Especially, I like the dark mode! ❤️

@hudochenkov
Copy link
Member

hudochenkov commented Jun 1, 2021

Great job!

Is it right that Docusaurus is no longer a static website generator? Or it could be configured as such?

@jeddy3
Copy link
Member Author

jeddy3 commented Jun 1, 2021

Is it right that Docusaurus is not longer a static website generator?

It's an SSG with client-side routing kicking in once the requested page has downloaded. Best of both worlds, e.g. works without JavaScript and good SEO, and then fast client-side routing if JavaScript is available.

The Netlify tab here shows it building 246 HTML pages.

@hudochenkov
Copy link
Member

It's an SSG with client-side routing kicking in once the requested page has downloaded. Best of both worlds, e.g. works without JavaScript and good SEO, and then fast client-side routing if JavaScript is available.

I had line wrapping disabled in View Source, and didn't notice full HTML, that I was expecting 🤦

@jeddy3 jeddy3 merged commit f8d3ff1 into master Jun 1, 2021
@jeddy3 jeddy3 deleted the doc-2 branch June 1, 2021 20:51
Copy link
Member

@m-allanson m-allanson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice stuff 🎉

@jeddy3
Copy link
Member Author

jeddy3 commented Jun 1, 2021

@m-allanson Thanks for doing the bulk of the migration in the previous pull request!

@ybiquitous & @hudochenkov thanks for the contributions and reviews.

The site is now live.

(The Algolia search will reindex within 24 hours, which will fix the 404 when searching for rules. You have to refresh the page after a search to see them at the moment.)

@slorber
Copy link

slorber commented Jun 2, 2021

Great!

Please add your site to our showcase ;)
https://docusaurus.io/showcase

For Algolia Search, I forgot to document this and will add it right now, but you should also ask the DocSearch team to update your config so that it works for Docusaurus v2, and they can trigger a re-index manually if needed. cc @shortcuts

This config may work but is not ideal for Docusaurus v2:
https://github.com/algolia/docsearch-configs/blob/master/configs/stylelint.json

@shortcuts
Copy link

Thanks for the heads up @slorber

I've made the update and the new results are now live!

@slorber
Copy link

slorber commented Jun 2, 2021

Thanks 🥇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Update to Docusaurus 2
6 participants