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

doc: fix links to ECMAScript module resolution #52883

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions doc/api/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ the `require.resolve()` function.
Putting together all of the above, here is the high-level algorithm
in pseudocode of what `require()` does:

<pre>
Copy link
Member

Choose a reason for hiding this comment

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

Why was this changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is covered in the commit message prior to this change being introduced: 490b02a. What should I change in there to better articulate the issue?

```pre html
require(X) from module at path Y
1. If X is a core module,
a. return the core module
Expand All @@ -256,7 +256,7 @@ MAYBE_DETECT_AND_LOAD(X)
1. If X parses as a CommonJS module, load X as a CommonJS module. STOP.
2. Else, if `--experimental-require-module` and `--experimental-detect-module` are
enabled, and the source code of X can be parsed as ECMAScript module using
<a href="esm.md#resolver-algorithm-specification">DETECT_MODULE_SYNTAX defined in
<a href="esm.md#resolution-algorithm-specification">DETECT_MODULE_SYNTAX defined in
the ESM resolver</a>,
a. Load X as an ECMAScript module. STOP.
3. THROW the SyntaxError from attempting to parse X as CommonJS in 1. STOP.
Expand Down Expand Up @@ -318,7 +318,7 @@ LOAD_PACKAGE_IMPORTS(X, DIR)
2. If no scope was found, return.
3. If the SCOPE/package.json "imports" is null or undefined, return.
4. let MATCH = PACKAGE_IMPORTS_RESOLVE(X, pathToFileURL(SCOPE),
["node", "require"]) <a href="esm.md#resolver-algorithm-specification">defined in the ESM resolver</a>.
["node", "require"]) <a href="esm.md#resolution-algorithm-specification">defined in the ESM resolver</a>.
5. RESOLVE_ESM_MATCH(MATCH).

LOAD_PACKAGE_EXPORTS(X, DIR)
Expand All @@ -329,7 +329,7 @@ LOAD_PACKAGE_EXPORTS(X, DIR)
3. Parse DIR/NAME/package.json, and look for "exports" field.
4. If "exports" is null or undefined, return.
5. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(DIR/NAME), "." + SUBPATH,
`package.json` "exports", ["node", "require"]) <a href="esm.md#resolver-algorithm-specification">defined in the ESM resolver</a>.
`package.json` "exports", ["node", "require"]) <a href="esm.md#resolution-algorithm-specification">defined in the ESM resolver</a>.
6. RESOLVE_ESM_MATCH(MATCH)

LOAD_PACKAGE_SELF(X, DIR)
Expand All @@ -339,15 +339,15 @@ LOAD_PACKAGE_SELF(X, DIR)
4. If the SCOPE/package.json "name" is not the first segment of X, return.
5. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(SCOPE),
"." + X.slice("name".length), `package.json` "exports", ["node", "require"])
<a href="esm.md#resolver-algorithm-specification">defined in the ESM resolver</a>.
<a href="esm.md#resolution-algorithm-specification">defined in the ESM resolver</a>.
6. RESOLVE_ESM_MATCH(MATCH)

RESOLVE_ESM_MATCH(MATCH)
1. let RESOLVED_PATH = fileURLToPath(MATCH)
2. If the file at RESOLVED_PATH exists, load RESOLVED_PATH as its extension
format. STOP
3. THROW "not found"
</pre>
```

## Caching

Expand Down
7 changes: 4 additions & 3 deletions doc/contributing/api-documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ The Node.js API documentation is generated by an in-house tooling that resides
within the [tools/doc](https://github.com/nodejs/node/tree/main/tools/doc)
directory.

The build process (using `make doc`) uses this tooling to parse the markdown
files in [doc/api](https://github.com/nodejs/node/tree/main/doc/api) and
generate the following:
The build process (using `make doc` or `make doc-only`) uses this tooling to
parse the markdown files in [doc/api][] and generate the following:

1. Human-readable HTML in `out/doc/api/*.html`
2. A JSON representation in `out/doc/api/*.json`
Expand Down Expand Up @@ -302,3 +301,5 @@ mutate the data and appends it to the final JSON object.

For a more in-depth information we recommend to refer to the `json.mjs` file as
it contains a lot of comments.

[doc/api]: https://github.com/nodejs/node/tree/main/doc/api
3 changes: 3 additions & 0 deletions tools/doc/html.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ function linkJsTypeDocs(text) {
}

const isJSFlavorSnippet = (node) => node.lang === 'cjs' || node.lang === 'mjs';
const isPreSnippet = (node) => node.lang === 'pre';

// Preprocess headers, stability blockquotes, and YAML blocks.
export function preprocessElements({ filename }) {
Expand Down Expand Up @@ -265,6 +266,8 @@ export function preprocessElements({ filename }) {
// Isolated JS snippet, no need to add the checkbox.
node.value = `<pre>${highlighted} ${copyButton}</pre>`;
}
} else if (isPreSnippet(node)) {
node.value = `<pre>${node.value}</pre>`;
} else {
node.value = `<pre>${highlighted} ${copyButton}</pre>`;
}
Expand Down
9 changes: 9 additions & 0 deletions tools/doc/markdown.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { visit } from 'unist-util-visit';

export const referenceToLocalMdFile = /^(?![+a-z]+:)([^#?]+)\.md(#.+)?$/i;
export const referenceToLocalMdFileInPre = /<a href="([^#"]+)\.md(#[^"]+)?">/g;

export function replaceLinks({ filename, linksMapper }) {
return (tree) => {
Expand All @@ -14,6 +15,14 @@ export function replaceLinks({ filename, linksMapper }) {
);
}
});
visit(tree, 'code', (node) => {
if (node.meta === 'html') {
node.value = node.value.replace(
referenceToLocalMdFileInPre,
(_, path, fragment) => `<a href="${path}.html${fragment || ''}">`,
);
}
});
visit(tree, 'definition', (node) => {
const htmlUrl = fileHtmlUrls && fileHtmlUrls[node.identifier];

Expand Down
1 change: 1 addition & 0 deletions tools/lint-md/lint-md.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23409,6 +23409,7 @@ const plugins = [
"markdown",
"mjs",
"powershell",
"pre",
"r",
"text",
"ts",
Expand Down