Skip to content

Commit 6166ef5

Browse files
committed
build: update to marked v16 and resolve breakages
Updates the repo to the latest version of `marked` and resolves some breaking changes that required us to switch some files to ESM. As a result, I had to disable syntax highlighting for type aliases and constants in the API docs, because it would've required us to rework the dgeni setup as well.
1 parent 1bba607 commit 6166ef5

23 files changed

+160
-211
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122
"karma-parallel": "^0.3.1",
123123
"karma-sourcemap-loader": "^0.4.0",
124124
"magic-string": "0.30.17",
125-
"marked": "^15.0.12",
125+
"marked": "^16.0.0",
126126
"minimatch": "^10.0.3",
127127
"parse5": "^7.1.2",
128128
"postcss": "^8.4.17",

pnpm-lock.yaml

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/dgeni/docs-package.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {Package} from 'dgeni';
22
import {ReadTypeScriptModules} from 'dgeni-packages/typescript/processors/readTypeScriptModules';
33
import {Host} from 'dgeni-packages/typescript/services/ts-host/host';
44
import {TypeFormatFlags} from 'typescript';
5-
import {HighlightNunjucksExtension} from './nunjucks-tags/highlight';
65
import {patchLogService} from './patch-log-service';
76
import {AsyncFunctionsProcessor} from './processors/async-functions';
87
import {categorizer} from './processors/categorizer';
@@ -165,6 +164,4 @@ apiDocsPackage.config(function (templateFinder: any, templateEngine: any) {
165164
variableStart: '{$',
166165
variableEnd: '$}',
167166
};
168-
169-
templateEngine.tags.push(new HighlightNunjucksExtension());
170167
});

tools/dgeni/nunjucks-tags/highlight.ts

Lines changed: 0 additions & 36 deletions
This file was deleted.

tools/dgeni/templates/constant.template.html

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ <h4 id="{$ constant.name $}" class="docs-header-link docs-api-h4 docs-api-consta
1717

1818
<div class="docs-markdown">
1919
<pre class="docs-markdown-pre">
20-
<code class="docs-markdown-code">
21-
{%- highlight "typescript" -%}
22-
const {$ constant.name | safe $}: {$ constant.type | safe $};
23-
{%- end_highlight -%}
24-
</code>
20+
<code class="docs-markdown-code">const {$ constant.name | safe $}: {$ constant.type | safe $};</code>
2521
</pre>
2622
</div>

tools/dgeni/templates/type-alias.template.html

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ <h4 id="{$ alias.name $}" class="docs-header-link docs-api-h4 docs-api-type-alia
1717

1818
<div class="docs-markdown">
1919
<pre class="docs-markdown-pre">
20-
<code class="docs-markdown-code">
21-
{%- highlight "typescript" -%}
22-
type {$ alias.name | safe $} = {$ alias.typeDefinition | safe $};
23-
{%- end_highlight -%}
24-
</code>
20+
<code class="docs-markdown-code">type {$ alias.name | safe $} = {$ alias.typeDefinition | safe $};</code>
2521
</pre>
2622
</div>

tools/extract-tokens/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package(default_visibility = ["//visibility:public"])
55

66
ts_project(
77
name = "extract_tokens_lib",
8-
srcs = glob(["**/*.ts"]),
8+
srcs = glob(["**/*.mts"]),
99
tsconfig = "//tools:tsconfig",
1010
deps = [
1111
"//:node_modules/sass",
@@ -18,5 +18,5 @@ js_binary(
1818
data = [
1919
":extract_tokens_lib",
2020
],
21-
entry_point = ":extract-tokens.js",
21+
entry_point = ":extract-tokens.mjs",
2222
)

tools/extract-tokens/extract-tokens.ts renamed to tools/extract-tokens/extract-tokens.mts

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {readFileSync, writeFileSync} from 'fs';
22
import {pathToFileURL} from 'url';
33
import {relative, join, dirname} from 'path';
44
import {compileString} from 'sass';
5-
import {highlightCodeBlock} from '../highlight-files/highlight-code-block';
5+
import {highlightCodeBlock} from '../highlight-files/highlight-code-block.mjs';
66

77
/** Information extracted for a single token from the theme. */
88
interface ExtractedToken {
@@ -43,40 +43,39 @@ interface ThemeData {
4343
}
4444

4545
// Script that extracts the tokens from a specific Bazel target.
46-
if (require.main === module) {
47-
const [packagePath, outputPath, ...inputFiles] = process.argv.slice(2);
48-
const themeFiles = inputFiles
49-
// Filter out only the files within the package
50-
// since the path also includes dependencies.
51-
.filter(file => file.startsWith(packagePath))
52-
.map(file => {
53-
// Assumption: all theme files start with an underscore since they're
54-
// partials and they end with `-theme`.
55-
// Assumption: the name under which the theme mixin will be available is the
56-
// same as the file name without the underscore and `-theme.scss`.
57-
const match = file.match(/_(.*)-theme\.scss$/);
58-
return match ? {mixinPrefix: match[1], filePath: file} : null;
59-
})
60-
.filter(file => !!file);
61-
62-
if (themeFiles.length === 0) {
63-
throw new Error(`Could not find theme files in ${packagePath}`);
64-
}
6546

66-
const themes: ThemeData[] = [];
47+
const [packagePath, outputPath, ...inputFiles] = process.argv.slice(2);
48+
const themeFiles = inputFiles
49+
// Filter out only the files within the package
50+
// since the path also includes dependencies.
51+
.filter(file => file.startsWith(packagePath))
52+
.map(file => {
53+
// Assumption: all theme files start with an underscore since they're
54+
// partials and they end with `-theme`.
55+
// Assumption: the name under which the theme mixin will be available is the
56+
// same as the file name without the underscore and `-theme.scss`.
57+
const match = file.match(/_(.*)-theme\.scss$/);
58+
return match ? {mixinPrefix: match[1], filePath: file} : null;
59+
})
60+
.filter(file => !!file);
61+
62+
if (themeFiles.length === 0) {
63+
throw new Error(`Could not find theme files in ${packagePath}`);
64+
}
65+
66+
const themes: ThemeData[] = [];
6767

68-
themeFiles.forEach(theme => {
69-
themes.push({
70-
name: theme.mixinPrefix,
71-
// This can be derived from the `name` already, but we want the source
72-
// of truth to be in this repo, instead of whatever page consumes the data.
73-
overridesMixin: `${theme.mixinPrefix}-overrides`,
74-
tokens: extractTokens(theme.filePath),
75-
});
68+
themeFiles.forEach(theme => {
69+
themes.push({
70+
name: theme.mixinPrefix,
71+
// This can be derived from the `name` already, but we want the source
72+
// of truth to be in this repo, instead of whatever page consumes the data.
73+
overridesMixin: `${theme.mixinPrefix}-overrides`,
74+
tokens: extractTokens(theme.filePath),
7675
});
76+
});
7777

78-
writeFileSync(outputPath, JSON.stringify({example: getUsageExample(themes), themes}));
79-
}
78+
writeFileSync(outputPath, JSON.stringify({example: getUsageExample(themes), themes}));
8079

8180
/**
8281
* Extracts the tokens from a theme file.

tools/highlight-files/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package(default_visibility = ["//visibility:public"])
55

66
ts_project(
77
name = "sources",
8-
srcs = glob(["**/*.ts"]),
8+
srcs = glob(["**/*.mts"]),
99
tsconfig = "//tools:tsconfig",
1010
deps = [
1111
"//:node_modules/@types/fs-extra",
@@ -20,5 +20,5 @@ js_binary(
2020
data = [
2121
":sources",
2222
],
23-
entry_point = ":highlight-files.js",
23+
entry_point = ":highlight-files.mjs",
2424
)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Script that will be used by the highlight_files Bazel rule in order to highlight
3+
* multiple input files using highlight.js. The output will be HTML files.
4+
*/
5+
6+
import fsExtra from 'fs-extra';
7+
import {dirname, extname, join, relative} from 'path';
8+
import {highlightCodeBlock} from './highlight-code-block.mjs';
9+
import {regionParser} from '../region-parser/region-parser.mjs';
10+
11+
/**
12+
* Determines the command line arguments for the current Bazel action. Since this action can
13+
* have a large set of input files, Bazel may write the arguments into a parameter file.
14+
* This function is responsible for handling normal argument passing or Bazel parameter files.
15+
* Read more here: https://docs.bazel.build/versions/master/skylark/lib/Args.html#use_param_file
16+
*/
17+
function getBazelActionArguments() {
18+
const args = process.argv.slice(2);
19+
20+
// If Bazel uses a parameter file, we've specified that it passes the file in the following
21+
// format: "arg0 arg1 --param-file={path_to_param_file}"
22+
if (args[0].startsWith('--param-file=')) {
23+
return fsExtra.readFileSync(args[0].split('=')[1], 'utf8').trim().split('\n');
24+
}
25+
26+
return args;
27+
}
28+
29+
function detectAndHighlightRegionBlocks(
30+
parsed: {contents: string; regions: {[p: string]: string}},
31+
basePath: string,
32+
outDir: string,
33+
) {
34+
const fileExtension = extname(basePath).substring(1);
35+
for (const [regionName, regionSnippet] of Object.entries(parsed.regions)) {
36+
// Create files for each found region
37+
if (!regionName) {
38+
continue;
39+
}
40+
const highlightedRegion = highlightCodeBlock(regionSnippet, fileExtension);
41+
// Convert "my-component-example.ts" into "my-component-example_region-ts.html"
42+
const regionBaseOutputPath = basePath.replace(
43+
`.${fileExtension}`,
44+
`_${regionName}-${fileExtension}.html`,
45+
);
46+
const regionOutputPath = join(outDir, regionBaseOutputPath);
47+
fsExtra.ensureDirSync(dirname(regionOutputPath));
48+
fsExtra.writeFileSync(regionOutputPath, highlightedRegion);
49+
}
50+
}
51+
52+
// The script expects the output directory as first argument. Second is the name of the
53+
// package where this the highlight target is declared. All remaining arguments will be
54+
// considered as markdown input files that need to be transformed.
55+
const [outDir, packageName, ...inputFiles] = getBazelActionArguments();
56+
57+
// Walk through each input file and write transformed markdown output
58+
// to the specified output directory.
59+
for (const execPath of inputFiles) {
60+
// Compute a relative path from the package to the actual input file.
61+
// e.g `src/components-examples/cdk/<..>/example.ts` becomes `cdk/<..>/example.ts`.
62+
const basePath = relative(packageName, execPath);
63+
const fileExtension = extname(basePath).substring(1);
64+
const parsed = regionParser(fsExtra.readFileSync(execPath, 'utf8'), fileExtension);
65+
detectAndHighlightRegionBlocks(parsed, basePath, outDir);
66+
// Convert "my-component-example.ts" into "my-component-example-ts.html"
67+
const baseOutputPath = basePath.replace(`.${fileExtension}`, `-${fileExtension}.html`);
68+
const outputPath = join(outDir, baseOutputPath);
69+
const htmlOutput = highlightCodeBlock(parsed.contents, fileExtension);
70+
71+
fsExtra.ensureDirSync(dirname(outputPath));
72+
fsExtra.writeFileSync(outputPath, htmlOutput);
73+
}

tools/highlight-files/highlight-files.ts

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)