-
-
Notifications
You must be signed in to change notification settings - Fork 16
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
Conversation
Deploy preview is at: 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
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: |
@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 For example:
A similar transform is used here. The logic would be:
I think that'll cover all cases we have in our rule READMEs. We'd then be able to do: Which is less noisy and easier to scan than emojis, especially when a rule has many options. |
@jeddy3 Looks good! I will look into the way. 💪🏼 |
FYI, the styling in the screenshot above was achieved by updating .valid-pattern .prism-code {
box-shadow: 0 -2px 0 0 green;
}
.invalid-pattern .prism-code {
box-shadow: 0 -2px 0 0 crimson;
} |
@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();
|
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 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. |
I think that's everything done now. Whatcha think, shall we push it live? |
There was a problem hiding this 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! ❤️
Great job! Is it right that Docusaurus is no longer a static website generator? Or it could be configured as such? |
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. |
I had line wrapping disabled in View Source, and didn't notice full HTML, that I was expecting 🤦 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice stuff 🎉
@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.) |
Great! Please add your site to our 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: |
Thanks for the heads up @slorber I've made the update and the new results are now live! |
Thanks 🥇 |
Closes #125
Supercedes #174
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.