Skip to content

Commit

Permalink
tools: fix CJS/ESM toggle on small screens
Browse files Browse the repository at this point in the history
PR-URL: #43506
Co-authored-by: Moshe Atlow <[email protected]>
Fixes: #43468
Reviewed-By: LiviaMedeiros <[email protected]>
  • Loading branch information
2 people authored and targos committed Jul 31, 2022
1 parent aab5adb commit a5089c9
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
3 changes: 3 additions & 0 deletions doc/api_assets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,9 @@ kbd {
background-repeat: no-repeat;
width: 142px;
height: 20px;
display: block;
cursor: pointer;
margin: 0;
}
.js-flavor-selector:checked {
background-image: url(./js-flavor-esm.svg);
Expand Down
1 change: 1 addition & 0 deletions doc/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<link rel="stylesheet" href="assets/hljs.css">
<link rel="canonical" href="https://nodejs.org/api/__FILENAME__.html">
<script async defer src="assets/api.js" type="text/javascript"></script>
__JS_FLAVORED_DYNAMIC_CSS__
</head>
<body class="alt apidoc" id="api-section-__FILENAME__">
<div id="content" class="clearfix">
Expand Down
10 changes: 9 additions & 1 deletion tools/doc/allhtml.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// of the generated html files.

import fs from 'fs';
import buildCSSForFlavoredJS from './buildCSSForFlavoredJS.mjs';

const source = new URL('../../out/doc/api/', import.meta.url);

Expand Down Expand Up @@ -90,7 +91,14 @@ all = all.slice(0, tocStart.index + tocStart[0].length) +
// Replace apicontent with the concatenated set of apicontents from each source.
const apiStart = /<\w+ id="apicontent">\s*/.exec(all);
const apiEnd = all.lastIndexOf('<!-- API END -->');
all = all.slice(0, apiStart.index + apiStart[0].length) +
all = all.slice(0, apiStart.index + apiStart[0].length)
.replace(
'\n</head>',
buildCSSForFlavoredJS(new Set(Array.from(
apicontent.matchAll(/(?<=<pre class="with-)\d+(?=-chars">)/g),
(x) => Number(x[0])
))) + '\n</head>'
) +
apicontent +
all.slice(apiEnd);

Expand Down
53 changes: 53 additions & 0 deletions tools/doc/buildCSSForFlavoredJS.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const CHAR_THRESHOLD = 68; // Around 68 characters, we have to take into
// account the left column that appears on screen
// wider than 1024px.

const ESTIMATED_CHAR_WIDTH = 8; // If the root element font-size is 16px (default value), 1ch is 7.8025px.
const TOGGLE_WIDTH = 142; // https://github.com/nodejs/node/blob/dbd938549b16718f2e4cc2809746b8c44a191b1d/doc/api_assets/style.css#L954

const PRE_MARGIN_LEFT = 16; // https://github.com/nodejs/node/blob/dbd938549b16718f2e4cc2809746b8c44a191b1d/doc/api_assets/style.css#L516
const PRE_MARGIN_RIGHT = 16; // https://github.com/nodejs/node/blob/dbd938549b16718f2e4cc2809746b8c44a191b1d/doc/api_assets/style.css#L516
const PRE_PADDING_LEFT = 16; // https://github.com/nodejs/node/blob/dbd938549b16718f2e4cc2809746b8c44a191b1d/doc/api_assets/style.css#L513
const PRE_PADDING_RIGHT = 16; // https://github.com/nodejs/node/blob/dbd938549b16718f2e4cc2809746b8c44a191b1d/doc/api_assets/style.css#L513


const COLUMN_RIGHT_MARGIN_LEFT_LARGE_SCREEN = 234; // https://github.com/nodejs/node/blob/dbd938549b16718f2e4cc2809746b8c44a191b1d/doc/api_assets/style.css#L653
const COLUMN_RIGHT_MARGIN_LEFT_SMALL_SCREEN = 0; // https://github.com/nodejs/node/blob/dbd938549b16718f2e4cc2809746b8c44a191b1d/doc/api_assets/style.css#L906
const COLUMN_RIGHT_MARGIN_RIGHT_LARGE_SCREEN = 0;
const COLUMN_RIGHT_MARGIN_RIGHT_SMALL_SCREEN = 0;
const COLUMN_RIGHT_PADDING_LEFT_LARGE_SCREEN = 24; // https://github.com/nodejs/node/blob/dbd938549b16718f2e4cc2809746b8c44a191b1d/doc/api_assets/style.css#L655
const COLUMN_RIGHT_PADDING_LEFT_SMALL_SCREEN = 8; // https://github.com/nodejs/node/blob/dbd938549b16718f2e4cc2809746b8c44a191b1d/doc/api_assets/style.css#L907
const COLUMN_RIGHT_PADDING_RIGHT_LARGE_SCREEN = 32; // https://github.com/nodejs/node/blob/dbd938549b16718f2e4cc2809746b8c44a191b1d/doc/api_assets/style.css#L654
const COLUMN_RIGHT_PADDING_RIGHT_SMALL_SCREEN = 8; // https://github.com/nodejs/node/blob/dbd938549b16718f2e4cc2809746b8c44a191b1d/doc/api_assets/style.css#L908

const getMarginLeft = (charCount) =>
(charCount > CHAR_THRESHOLD ?
COLUMN_RIGHT_MARGIN_LEFT_LARGE_SCREEN :
COLUMN_RIGHT_MARGIN_LEFT_SMALL_SCREEN) + PRE_MARGIN_LEFT;
const getPaddingLeft = (charCount) =>
(charCount > CHAR_THRESHOLD ?
COLUMN_RIGHT_PADDING_LEFT_LARGE_SCREEN :
COLUMN_RIGHT_PADDING_LEFT_SMALL_SCREEN) + PRE_PADDING_LEFT;
const getPaddingRight = (charCount) =>
(charCount > CHAR_THRESHOLD ?
COLUMN_RIGHT_PADDING_RIGHT_LARGE_SCREEN :
COLUMN_RIGHT_PADDING_RIGHT_SMALL_SCREEN) + PRE_PADDING_RIGHT;
const getMarginRight = (charCount) =>
(charCount > CHAR_THRESHOLD ?
COLUMN_RIGHT_MARGIN_RIGHT_LARGE_SCREEN :
COLUMN_RIGHT_MARGIN_RIGHT_SMALL_SCREEN) + PRE_MARGIN_RIGHT;


export default function buildCSSForFlavoredJS(dynamicSizes) {
if (dynamicSizes == null || dynamicSizes.length === 0) return '';

return `<style>${Array.from(dynamicSizes, (charCount) =>
`@media(max-width:${getMarginLeft(charCount) + getPaddingLeft(charCount) +
charCount * ESTIMATED_CHAR_WIDTH + TOGGLE_WIDTH +
getPaddingRight(charCount) + getMarginRight(charCount)}px){` +
`.with-${charCount}-chars>.js-flavor-selector{` +
'float:none;' +
'margin:0 0 1em auto;' +
'}' +
'}').join('')}</style>`;
}
13 changes: 12 additions & 1 deletion tools/doc/html.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ import { visit } from 'unist-util-visit';

import * as common from './common.mjs';
import * as typeParser from './type-parser.mjs';
import buildCSSForFlavoredJS from './buildCSSForFlavoredJS.mjs';

const dynamicSizes = Object.create(null);

const { highlight, getLanguage } = highlightJs;

Expand Down Expand Up @@ -90,6 +93,8 @@ function processContent(content) {
}

export function toHTML({ input, content, filename, nodeVersion, versions }) {
const dynamicSizesForThisFile = dynamicSizes[filename];

filename = path.basename(filename, '.md');

const id = filename.replace(/\W+/g, '-');
Expand All @@ -99,6 +104,7 @@ export function toHTML({ input, content, filename, nodeVersion, versions }) {
.replace('__SECTION__', content.section)
.replace(/__VERSION__/g, nodeVersion)
.replace(/__TOC__/g, content.toc)
.replace('__JS_FLAVORED_DYNAMIC_CSS__', buildCSSForFlavoredJS(dynamicSizesForThisFile))
.replace(/__TOC_PICKER__/g, tocPicker(id, content))
.replace(/__GTOC_PICKER__/g, gtocPicker(id))
.replace(/__GTOC__/g, gtocHTML.replace(
Expand Down Expand Up @@ -228,14 +234,19 @@ export function preprocessElements({ filename }) {
const previousNode = parent.children[index - 1] || {};
const nextNode = parent.children[index + 1] || {};

const charCountFirstTwoLines = Math.max(...node.value.split('\n', 2).map((str) => str.length));

if (!isJSFlavorSnippet(previousNode) &&
isJSFlavorSnippet(nextNode) &&
nextNode.lang !== node.lang) {
// Saving the highlight code as value to be added in the next node.
node.value = highlighted;
node.charCountFirstTwoLines = charCountFirstTwoLines;
} else if (isJSFlavorSnippet(previousNode) &&
previousNode.lang !== node.lang) {
node.value = '<pre>' +
const actualCharCount = Math.max(charCountFirstTwoLines, previousNode.charCountFirstTwoLines);
(dynamicSizes[filename] ??= new Set()).add(actualCharCount);
node.value = `<pre class="with-${actualCharCount}-chars">` +
'<input class="js-flavor-selector" type="checkbox"' +
// If CJS comes in second, ESM should display by default.
(node.lang === 'cjs' ? ' checked' : '') +
Expand Down

0 comments on commit a5089c9

Please sign in to comment.