Skip to content

Commit d5e1f9b

Browse files
Connor ClarkDevtools-frontend LUCI CQ
Connor Clark
authored and
Devtools-frontend LUCI CQ
committed
[RPP] Support markdown in insights
This makes it simpler to localize strings with links or code tags. Using markdown inside an i18n string is new for CDT, but not for the i18n library DevTools uses (which was taken from Lighthouse, and markdown is used liberally there). There's nothing special to do to use Markdown, all the markdown-specific bits are transformed to placeholders for the i18n pipeline like so: "panels/timeline/components/insights/Viewport.ts | description": { "message": "A viewport meta element not only optimizes your app for mobile screen sizes, but also $LINK_START_0$prevents a 300 millisecond delay to user input$LINK_END_0$.", "description": "Text to tell the user how a viewport meta element can improve performance.", "placeholders": { "LINK_START_0": { "content": "[" }, "LINK_END_0": { "content": "](https://developer.chrome.com/blog/300ms-tap-delay-gone-away/)" } } }, This patch utilizes the existing MarkdownView component, but with a helper that allows rendering given just the markdown string. Bug: None Change-Id: I2cc1af7aed2918cde090c69d9ef7c8a3a1fd4622 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/5800335 Commit-Queue: Connor Clark <[email protected]> Reviewed-by: Simon Zünd <[email protected]>
1 parent 95d092d commit d5e1f9b

File tree

5 files changed

+26
-7
lines changed

5 files changed

+26
-7
lines changed

front_end/panels/timeline/components/insights/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ devtools_module("insights") {
3131
"../../../../core/platform:bundle",
3232
"../../../../models/trace:bundle",
3333
"../../../../services/trace_bounds:bundle",
34+
"../../../../third_party/marked:bundle",
3435
"../../../../ui/components/buttons:bundle",
3536
"../../../../ui/components/helpers:bundle",
3637
"../../../../ui/components/icon_button:bundle",
38+
"../../../../ui/components/markdown_view:bundle",
3739
"../../../../ui/lit-html:bundle",
3840
"../../overlays:bundle",
3941
]

front_end/panels/timeline/components/insights/Helpers.ts

+16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
// Copyright 2024 The Chromium Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
4+
45
import type * as TraceEngine from '../../../../models/trace/trace.js';
6+
import * as Marked from '../../../../third_party/marked/marked.js';
57
import * as ComponentHelpers from '../../../../ui/components/helpers/helpers.js';
8+
import * as MarkdownView from '../../../../ui/components/markdown_view/markdown_view.js';
9+
import * as LitHtml from '../../../../ui/lit-html/lit-html.js';
610
import type * as Overlays from '../../overlays/overlays.js';
711

812
import sidebarInsightStyles from './sidebarInsight.css.js';
@@ -108,3 +112,15 @@ export abstract class BaseInsight extends HTMLElement {
108112
});
109113
}
110114
}
115+
116+
/**
117+
* Returns a rendered MarkdownView component.
118+
*
119+
* This should not be used for markdown that is not guaranteed to be valid.
120+
*/
121+
export function md(markdown: string): LitHtml.TemplateResult {
122+
const tokens = Marked.Marked.lexer(markdown);
123+
return LitHtml.html`<${MarkdownView.MarkdownView.MarkdownView.litTagName}
124+
.data=${{tokens} as MarkdownView.MarkdownView.MarkdownViewData}>
125+
</${MarkdownView.MarkdownView.MarkdownView.litTagName}>`;
126+
}

front_end/panels/timeline/components/insights/Viewport.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type * as TraceEngine from '../../../../models/trace/trace.js';
77
import * as LitHtml from '../../../../ui/lit-html/lit-html.js';
88
import type * as Overlays from '../../overlays/overlays.js';
99

10-
import {BaseInsight, shouldRenderForCategory} from './Helpers.js';
10+
import {BaseInsight, md, shouldRenderForCategory} from './Helpers.js';
1111
import {NodeLink, type NodeLinkData} from './NodeLink.js';
1212
import * as SidebarInsight from './SidebarInsight.js';
1313
import {InsightsCategories} from './types.js';
@@ -17,7 +17,7 @@ const UIStrings = {
1717
* @description Text to tell the user how a viewport meta element can improve performance.
1818
*/
1919
description: 'A viewport meta element not only optimizes your app for mobile screen sizes, ' +
20-
'but also prevents a 300 millisecond delay to user input.',
20+
'but also [prevents a 300 millisecond delay to user input](https://developer.chrome.com/blog/300ms-tap-delay-gone-away/).',
2121
};
2222

2323
const str_ = i18n.i18n.registerUIStrings('panels/timeline/components/insights/Viewport.ts', UIStrings);
@@ -65,10 +65,7 @@ export class Viewport extends BaseInsight {
6565
} as SidebarInsight.InsightDetails}
6666
@insighttoggleclick=${this.onSidebarClick}>
6767
<div slot="insight-description" class="insight-description">
68-
<span>${i18nString(UIStrings.description)}</span>
69-
<div>
70-
<x-link class="link" href="https://developer.chrome.com/blog/300ms-tap-delay-gone-away/">Learn more here.</x-link>
71-
</div>
68+
${md(i18nString(UIStrings.description))}
7269
</div>
7370
<div slot="insight-content" class="insight-content">
7471
${backendNodeId !== undefined ? LitHtml.html`<${NodeLink.litTagName}

front_end/third_party/marked/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ devtools_entrypoint("bundle") {
2626
"../../models/issues_manager/*",
2727
"../../panels/explain/*",
2828
"../../panels/freestyler/*",
29+
"../../panels/timeline/*",
2930
"../../ui/components/docs/console_insight/*",
3031
"../../ui/components/docs/freestyler/*",
3132
"../../ui/components/docs/markdown_view/*",

front_end/ui/components/markdown_view/MarkdownLinksMap.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ export const markdownLinks = new Map<string, string>([
5656
]);
5757

5858
export const getMarkdownLink = (key: string): string => {
59-
if (/^https:\/\/www.chromestatus.com\/feature\/\d+$/.test(key)) {
59+
if (/^https:\/\/www\.chromestatus\.com\/feature\/\d+$/.test(key)) {
60+
return key;
61+
}
62+
if (/^https:\/\/developer\.chrome\.com\/blog\//.test(key)) {
6063
return key;
6164
}
6265
const link = markdownLinks.get(key);

0 commit comments

Comments
 (0)