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

Added CodeBlockComponent #2275

Merged
merged 2 commits into from Dec 21, 2020
Merged

Added CodeBlockComponent #2275

merged 2 commits into from Dec 21, 2020

Conversation

oliverlloyd
Copy link
Contributor

What?

Adds the CodeBlockElement to display code snippets inside articles.

Support for code snippets has been fairly basic up to now on Frontend, we set a background colour, the font, some spacing and that's about it. In this PR I wanted to update these styles and also add syntax highlighting.

Why?

More readability, better accessibility and so that our articles in the Guardian Digital Blog look nicer

Before

CodeBlockElement - Before

After

CodeBlockElement - After

Library choice

Given code formatting is a specialist field with mature solutions already available, I decided to use a library. There are three main options available:

highlight.js
Google prettier
and
Prism

Highlight.js

Pros:

  • Used by Stackoverflow
  • Feature rich
  • Auto-detection
  • Supports SSR

Cons:

Google

I list this here because I see references to this in Frontend and it's well known but it is now deprecated.

Prism

Pros:

  • Lightweight
  • JS/TS focussed
  • Supports SSR

Cons:

  • You have to specify the language

Decision

Use Prism. Bundle size is critical for us and the result even when using the wrong language is still acceptable.

@github-actions
Copy link

github-actions bot commented Dec 21, 2020

Size Change: +1.18 kB (0%)

Total Size: 728 kB

Filename Size Change
dist/frontend.server.js 228 kB +1.18 kB (0%)
ℹ️ View Unchanged
Filename Size Change
dist/atomIframe.js 1.22 kB 0 B
dist/atomIframe.legacy.js 1.22 kB 0 B
dist/dynamicImport.js 1.97 kB 0 B
dist/dynamicImport.legacy.js 2.02 kB 0 B
dist/embedIframe.js 1.22 kB 0 B
dist/embedIframe.legacy.js 1.23 kB 0 B
dist/ga.js 3.02 kB 0 B
dist/ga.legacy.js 3.48 kB 0 B
dist/GetMatchStats.js 3.23 kB 0 B
dist/GetMatchStats.legacy.js 3.32 kB 0 B
dist/lotame.js 1.15 kB 0 B
dist/lotame.legacy.js 1.16 kB 0 B
dist/MostViewedFooterData.js 5.63 kB 0 B
dist/MostViewedFooterData.legacy.js 5.84 kB 0 B
dist/MostViewedRightWrapper.js 4.08 kB 0 B
dist/MostViewedRightWrapper.legacy.js 4.3 kB 0 B
dist/newsletterEmbedIframe.js 1.15 kB 0 B
dist/newsletterEmbedIframe.legacy.js 1.16 kB 0 B
dist/OnwardsLower.js 8.55 kB 0 B
dist/OnwardsLower.legacy.js 8.81 kB 0 B
dist/OnwardsUpper.js 12.4 kB 0 B
dist/OnwardsUpper.legacy.js 12.7 kB 0 B
dist/ophan.js 6.95 kB 0 B
dist/ophan.legacy.js 6.94 kB 0 B
dist/react.js 115 kB 0 B
dist/react.legacy.js 119 kB 0 B
dist/sentry.js 22.8 kB 0 B
dist/sentry.legacy.js 22.7 kB 0 B
dist/shimport.js 3.21 kB 0 B
dist/shimport.legacy.js 3.21 kB 0 B
dist/SignInGateMain.js 1.87 kB 0 B
dist/SignInGateMain.legacy.js 1.89 kB 0 B
dist/vendors~braze-web-sdk-core.js 34.9 kB 0 B
dist/vendors~braze-web-sdk-core.legacy.js 34.9 kB 0 B
dist/vendors~guardian-braze-components.js 18.6 kB 0 B
dist/vendors~guardian-braze-components.legacy.js 18.7 kB 0 B

compressed-size-action

Copy link
Contributor

@liywjl liywjl left a comment

Choose a reason for hiding this comment

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

LGTM, though unsure about babel being in dependencies rather than devDependencies

@@ -65,6 +65,7 @@
"@typescript-eslint/eslint-plugin-tslint": "^4.1.1",
"ajv": "^6.12.6",
"aws-sdk": "^2.804.0",
"babel-plugin-prismjs": "^2.0.1",
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldnt this be in devDependencies?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great spot! Fixed 👍

@oliverlloyd
Copy link
Contributor Author

CodeBlockElement

@oliverlloyd oliverlloyd merged commit 6d4d2cc into main Dec 21, 2020
@oliverlloyd oliverlloyd deleted the oliver/code-block branch December 21, 2020 16:35
@@ -24,6 +24,10 @@ module.exports = {
],
'babel-plugin-px-to-rem',
'@babel/plugin-transform-runtime',
["prismjs", {
// This list should match those defined in typescript type Language
"languages": ["typescript", "javascript", "css", "markup", "scala", "elm"]
Copy link
Contributor

Choose a reason for hiding this comment

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

For the record, here's the list currently supported by composer.

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should probably add typescript to that list. Is that easy to do?

Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure, first implementation in composer is here: https://github.com/guardian/flexible-content/pull/459. That makes it look like it's using pretiffy which may or may not support TS. To be fair, it might not matter that much.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Prettify is deprecated and I'm not sure if the implementation we have with it on Frontend is working anymore - the code blocks I see there don't seem to have much styling other than a basic pass.

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.

None yet

4 participants