Skip to content

Rebuild Custom code blocks languages #2338

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

Closed
wants to merge 3 commits into from

Conversation

montogeek
Copy link
Member

After 4 hours doing this I finally managed to solve it.

This PR introduces support for custom js-with-links code blocks.

File prism-links.js is a copy of https://github.com/montogeek/remark-refractor, once this PR is merged I will update the dependency.

File js-with-links.js will be moved to https://github.com/montogeek/remark-refractor as well.

One changed is necessary, update js-with-links to jslinks, I am not 100% sure, but it seems Prism doesn't like composed names.

Also updated our process readme script to replace weird code examples (console, sh) to bash.

Update our content removing wrong and unnecessary code languages.

@montogeek montogeek requested a review from jeremenichelli July 5, 2018 18:31

module.exports = jslinks;

jslinks.displayName = 'jslinks';
Copy link
Member Author

Choose a reason for hiding this comment

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

It seems this property should be the same as the code snippet language.

Copy link
Member

Choose a reason for hiding this comment

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

Should we keep all the commented code?

Copy link
Member Author

Choose a reason for hiding this comment

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

From the original PR text:

File js-with-links.js will be moved to https://github.com/montogeek/remark-refractor as well.

It means commented code will be deleted, as it is right now: https://github.com/montogeek/remark-refractor/blob/master/src/index.js

Copy link
Member Author

@montogeek montogeek left a comment

Choose a reason for hiding this comment

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

Need cleanup.

@@ -41,11 +41,6 @@
"serve": "npm run build && sirv start ./dist --port 4000",
"deploy": "gh-pages -d dist"
},
"husky": {
Copy link
Member Author

Choose a reason for hiding this comment

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

Will revert

Copy link
Member

Choose a reason for hiding this comment

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

👍

try {
data.hChildren = refractor.highlight(node.value, lang);

for (let index = 0; index < data.hChildren.length; index++) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I find all comments in the tree and update every one by inject a link element.

Copy link
Member

Choose a reason for hiding this comment

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

Can we rename the hChildren to something more meaningful. It can be just hastChildren or children, or hastNodes.

Copy link
Member Author

Choose a reason for hiding this comment

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

It can't, otherwise other libraries won't read it.
That is how it works: https://github.com/syntax-tree/mdast-util-to-hast/

}

try {
data.hChildren = refractor.highlight(node.value, lang);
Copy link
Member Author

Choose a reason for hiding this comment

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

refractor returns a hast tree.

if (element.properties && element.properties.className.includes('comment')) {
const { value } = element.children[0];

let match = /\[([^[\]]+?)\]\((.+?)\)/.exec(value);
Copy link
Member Author

Choose a reason for hiding this comment

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

This Regex find all comments, it is from markdown.js file.

Copy link
Member

Choose a reason for hiding this comment

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

Can we place a comment for this then?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah

if (match !== null) {
const [, text, url] = match;

element.children = [
Copy link
Member Author

Choose a reason for hiding this comment

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

Node children are:

  1. //
  2. An empty space
  3. Link

}
}

node.children = [node.data.hChildren];
Copy link
Member Author

Choose a reason for hiding this comment

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

Will removed this line, it is not necessary.

Copy link
Member

Choose a reason for hiding this comment

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

👍

.replace(/<!--[\s\S]*?-->/g, '');
.replace(/<!--[\s\S]*?-->/g, '')
// Replace code
.replace(/```console/g, '```bash')
Copy link
Member Author

Choose a reason for hiding this comment

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

This new 2 lines save a lot of Prism warnings.

Copy link
Member

Choose a reason for hiding this comment

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

do you think its worth it replacing content on the fly? i would suggest find replace console -> bash, or is this issue coming from external readme files we fetch?

Copy link
Member

Choose a reason for hiding this comment

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

I kinda have the feeling that we should open a PR in projects using console and change it instead of this because it is not a valid language option in markdown regarding highlighting.

Copy link
Member Author

Choose a reason for hiding this comment

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

@EugeneHlushko Yes, it is from external readmes.
@jeremenichelli Yep :D, will revert

Copy link
Member

Choose a reason for hiding this comment

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

We can keep this, until we replace it in all READMEs.

Copy link
Member Author

@montogeek montogeek Jul 19, 2018

Choose a reason for hiding this comment

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

I already talked to @shellscape, console is better because GitHub support is richer than bash.
This is easily fixed with a plugin, just registering it using Prism:
Prism.languages['console'] = Prism.languages.extend('sh', {})

@montogeek montogeek requested a review from EugeneHlushko July 5, 2018 18:37
@montogeek montogeek changed the title Rebuild prism languages Rebuild Custom code blocks languages Jul 5, 2018
Copy link
Member

@jeremenichelli jeremenichelli left a comment

Choose a reason for hiding this comment

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

Great job man, just some minor comments to address.


module.exports = jslinks;

jslinks.displayName = 'jslinks';
Copy link
Member

Choose a reason for hiding this comment

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

Should we keep all the commented code?

@@ -41,11 +41,6 @@
"serve": "npm run build && sirv start ./dist --port 4000",
"deploy": "gh-pages -d dist"
},
"husky": {
Copy link
Member

Choose a reason for hiding this comment

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

👍

try {
data.hChildren = refractor.highlight(node.value, lang);

for (let index = 0; index < data.hChildren.length; index++) {
Copy link
Member

Choose a reason for hiding this comment

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

Can we rename the hChildren to something more meaningful. It can be just hastChildren or children, or hastNodes.

if (element.properties && element.properties.className.includes('comment')) {
const { value } = element.children[0];

let match = /\[([^[\]]+?)\]\((.+?)\)/.exec(value);
Copy link
Member

Choose a reason for hiding this comment

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

Can we place a comment for this then?

}
}

node.children = [node.data.hChildren];
Copy link
Member

Choose a reason for hiding this comment

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

👍

}

data.hProperties = data.hProperties || {};
data.hProperties.className = ['hljs', ...(data.hProperties.className || []), `language-${lang}`];
Copy link
Member

Choose a reason for hiding this comment

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

Same as above, can we rename the hProperties to something more readable?

Copy link
Member Author

Choose a reason for hiding this comment

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

We can't, same reason as hChildren, that is how unified ecosystem works.

.replace(/<!--[\s\S]*?-->/g, '');
.replace(/<!--[\s\S]*?-->/g, '')
// Replace code
.replace(/```console/g, '```bash')
Copy link
Member

Choose a reason for hiding this comment

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

We can keep this, until we replace it in all READMEs.

@montogeek montogeek mentioned this pull request Jul 22, 2018
16 tasks
@montogeek
Copy link
Member Author

I will revamp this PR, after working a bit more with details code blocks, I found some improvements.

@montogeek
Copy link
Member Author

Closing in favour of #2564

@montogeek montogeek closed this Dec 15, 2018
@montogeek montogeek deleted the rebuild-prism-languages branch December 15, 2018 22:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants