@salesforcedevs/docs-components 0.17.1 → 0.17.12-search-alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/lwc.config.json +17 -3
  2. package/package.json +18 -7
  3. package/src/modules/README.md +41 -0
  4. package/src/modules/doc/amfModelParser/amfModelParser.ts +674 -0
  5. package/src/modules/doc/amfReference/amfReference.css +25 -0
  6. package/src/modules/doc/amfReference/amfReference.html +55 -0
  7. package/src/modules/doc/amfReference/amfReference.ts +1467 -0
  8. package/src/modules/doc/amfReference/constants.ts +76 -0
  9. package/src/modules/doc/amfReference/types.ts +125 -0
  10. package/src/modules/doc/amfTopic/amfTopic.css +21 -0
  11. package/src/modules/doc/amfTopic/amfTopic.html +3 -0
  12. package/src/modules/doc/amfTopic/amfTopic.ts +111 -0
  13. package/src/modules/doc/amfTopic/types.ts +56 -0
  14. package/src/modules/doc/amfTopic/utils.ts +136 -0
  15. package/src/modules/doc/breadcrumbItem/breadcrumbItem.css +51 -0
  16. package/src/modules/doc/breadcrumbItem/breadcrumbItem.html +5 -0
  17. package/src/modules/doc/breadcrumbItem/breadcrumbItem.ts +71 -0
  18. package/src/modules/doc/breadcrumbs/breadcrumbs.css +27 -0
  19. package/src/modules/doc/breadcrumbs/breadcrumbs.html +58 -0
  20. package/src/modules/doc/breadcrumbs/breadcrumbs.ts +183 -0
  21. package/src/modules/doc/componentPlayground/componentPlayground.css +22 -0
  22. package/src/modules/doc/componentPlayground/componentPlayground.html +20 -0
  23. package/src/modules/doc/componentPlayground/componentPlayground.ts +42 -0
  24. package/src/modules/doc/content/content.css +89 -70
  25. package/src/modules/doc/content/content.html +1 -0
  26. package/src/modules/doc/content/content.ts +188 -195
  27. package/src/modules/doc/contentCallout/contentCallout.css +18 -10
  28. package/src/modules/doc/contentCallout/contentCallout.html +13 -4
  29. package/src/modules/doc/contentCallout/contentCallout.ts +14 -2
  30. package/src/modules/doc/contentLayout/contentLayout.css +1 -0
  31. package/src/modules/doc/contentLayout/contentLayout.html +46 -0
  32. package/src/modules/doc/contentLayout/contentLayout.ts +524 -0
  33. package/src/modules/doc/doDont/doDont.css +47 -0
  34. package/src/modules/doc/doDont/doDont.html +27 -0
  35. package/src/modules/doc/doDont/doDont.ts +17 -0
  36. package/src/modules/doc/header/header.css +70 -37
  37. package/src/modules/doc/header/header.html +41 -138
  38. package/src/modules/doc/header/header.ts +56 -78
  39. package/src/modules/doc/heading/heading.css +33 -0
  40. package/src/modules/doc/heading/heading.html +14 -0
  41. package/src/modules/doc/heading/heading.ts +67 -0
  42. package/src/modules/doc/headingAnchor/headingAnchor.css +3 -3
  43. package/src/modules/doc/headingAnchor/headingAnchor.ts +2 -2
  44. package/src/modules/doc/headingContent/headingContent.css +53 -0
  45. package/src/modules/doc/headingContent/headingContent.html +13 -0
  46. package/src/modules/doc/headingContent/headingContent.ts +30 -0
  47. package/src/modules/doc/lwcContentLayout/lwcContentLayout.css +1 -0
  48. package/src/modules/doc/lwcContentLayout/lwcContentLayout.html +45 -0
  49. package/src/modules/doc/lwcContentLayout/lwcContentLayout.ts +256 -0
  50. package/src/modules/doc/overview/overview.css +40 -0
  51. package/src/modules/doc/overview/overview.html +34 -0
  52. package/src/modules/doc/overview/overview.ts +12 -0
  53. package/src/modules/doc/phase/phase.css +21 -6
  54. package/src/modules/doc/phase/phase.html +15 -3
  55. package/src/modules/doc/phase/phase.ts +48 -12
  56. package/src/modules/doc/specificationContent/specificationContent.css +36 -0
  57. package/src/modules/doc/specificationContent/specificationContent.html +167 -0
  58. package/src/modules/doc/specificationContent/specificationContent.ts +127 -0
  59. package/src/modules/doc/sprigSurvey/sprigSurvey.html +20 -0
  60. package/src/modules/doc/sprigSurvey/sprigSurvey.scoped.css +16 -0
  61. package/src/modules/doc/sprigSurvey/sprigSurvey.ts +16 -0
  62. package/src/modules/doc/toc/toc.ts +1 -1
  63. package/src/modules/doc/versionPicker/versionPicker.css +64 -0
  64. package/src/modules/doc/versionPicker/versionPicker.html +38 -0
  65. package/src/modules/doc/versionPicker/versionPicker.ts +65 -0
  66. package/src/modules/doc/xmlContent/types.ts +120 -0
  67. package/src/modules/doc/xmlContent/utils.ts +163 -0
  68. package/src/modules/doc/xmlContent/xmlContent.css +54 -0
  69. package/src/modules/doc/xmlContent/xmlContent.html +54 -0
  70. package/src/modules/doc/xmlContent/xmlContent.ts +763 -0
  71. package/src/modules/docHelpers/amfStyle/amfStyle.css +355 -0
  72. package/src/modules/docHelpers/contentLayoutStyle/contentLayoutStyle.css +131 -0
  73. package/src/modules/docHelpers/imgStyle/imgStyle.css +59 -0
  74. package/src/modules/{helpers → docHelpers}/status/status.css +1 -1
  75. package/src/modules/docUtils/searchSyncer/searchSyncer.ts +86 -0
  76. package/src/modules/docUtils/utils/__mocks__/coveo.analytics.ts +16 -0
  77. package/src/modules/docUtils/utils/coveo.analytics.d.ts +10 -0
  78. package/src/modules/docUtils/utils/utils.ts +32 -0
  79. package/LICENSE +0 -12
  80. package/src/modules/doc/content/__tests__/content.test.ts +0 -312
  81. package/src/modules/doc/content/__tests__/mockDocContent.ts +0 -348
  82. package/src/modules/doc/content/__tests__/mockPageReference.ts +0 -8
  83. package/src/modules/doc/content/__tests__/mockSidebar.ts +0 -81
  84. package/src/modules/doc/content/content.stories.ts +0 -148
  85. package/src/modules/doc/contentCallout/__tests__/contentCallout.test.ts +0 -80
  86. package/src/modules/doc/contentCallout/__tests__/mockProps.ts +0 -14
  87. package/src/modules/doc/contentCallout/contentCallout.stories.ts +0 -29
  88. package/src/modules/doc/contentMedia/__tests__/contentMedia.test.ts +0 -97
  89. package/src/modules/doc/contentMedia/contentMedia.stories.ts +0 -113
  90. package/src/modules/doc/header/__tests__/coveoConfig.ts +0 -12
  91. package/src/modules/doc/header/__tests__/header.test.ts +0 -445
  92. package/src/modules/doc/header/__tests__/mockNavDevelopers.ts +0 -427
  93. package/src/modules/doc/header/__tests__/mockNavs.ts +0 -115
  94. package/src/modules/doc/header/__tests__/mockProps.ts +0 -152
  95. package/src/modules/doc/header/header.stories.ts +0 -190
  96. package/src/modules/doc/headingAnchor/__tests__/headingAnchor.test.ts +0 -111
  97. package/src/modules/doc/headingAnchor/headingAnchor.stories.ts +0 -33
  98. package/src/modules/doc/nav/__tests__/mockAvailableLanguages.ts +0 -8
  99. package/src/modules/doc/nav/__tests__/mockAvailableVersions.ts +0 -122
  100. package/src/modules/doc/nav/__tests__/mockPageReference.ts +0 -8
  101. package/src/modules/doc/nav/__tests__/mockPdfUrl.ts +0 -1
  102. package/src/modules/doc/nav/__tests__/mockSelectedLanguage.ts +0 -8
  103. package/src/modules/doc/nav/__tests__/mockSelectedVersion.ts +0 -8
  104. package/src/modules/doc/nav/__tests__/mockToc.ts +0 -146
  105. package/src/modules/doc/nav/__tests__/nav.test.ts +0 -58
  106. package/src/modules/doc/phase/__tests__/mockProps.ts +0 -13
  107. package/src/modules/doc/phase/__tests__/phase.test.ts +0 -60
  108. package/src/modules/doc/phase/phase.stories.ts +0 -12
  109. package/src/modules/doc/toc/__tests__/mockPageReference.ts +0 -8
  110. package/src/modules/doc/toc/__tests__/mockToc.ts +0 -146
  111. package/src/modules/doc/toc/__tests__/toc.test.ts +0 -29
  112. package/src/modules/doc/toolbar/__tests__/mockAvailableLanguages.ts +0 -8
  113. package/src/modules/doc/toolbar/__tests__/mockAvailableVersions.ts +0 -122
  114. package/src/modules/doc/toolbar/__tests__/mockPdfUrl.ts +0 -1
  115. package/src/modules/doc/toolbar/__tests__/mockSelectedLanguage.ts +0 -8
  116. package/src/modules/doc/toolbar/__tests__/mockSelectedVersion.ts +0 -8
  117. package/src/modules/doc/toolbar/__tests__/toolbar.test.ts +0 -44
@@ -0,0 +1,763 @@
1
+ /* eslint-disable @lwc/lwc/no-document-query */
2
+ import { api, track } from "lwc";
3
+ import { FetchContent } from "./utils";
4
+ import {
5
+ DocLanguage,
6
+ DocVersion,
7
+ TreeNode,
8
+ Header,
9
+ SiderbarFooter,
10
+ HistoryState,
11
+ PageReference,
12
+ TocMap,
13
+ ContentData
14
+ } from "./types";
15
+ import { SearchSyncer } from "docUtils/searchSyncer";
16
+ import { LightningElementWithState } from "dxBaseElements/lightningElementWithState";
17
+ import { oldVersionDocInfo } from "docUtils/utils";
18
+ import { Breadcrumb, DocPhaseInfo, Language } from "typings/custom";
19
+ import { track as trackGTM } from "dxUtils/analytics";
20
+ import DOMPurify from "dompurify";
21
+
22
+ // TODO: Imitating from actual implementation as doc-content use it like this. We should refactor it later.
23
+ const handleContentError = (error: any): void => console.log(error);
24
+
25
+ const PIXEL_PER_CHARACTER_MAP: { [key: string]: number } = {
26
+ default: 7.7,
27
+ "ja-jp": 12.5
28
+ };
29
+
30
+ const defaultSidebarFooter: SiderbarFooter = {
31
+ bailHref: "",
32
+ bailLabel: "",
33
+ languages: [],
34
+ language: ""
35
+ };
36
+ export default class DocXmlContent extends LightningElementWithState<{
37
+ isFetchingDocument: boolean;
38
+ isFetchingContent: boolean;
39
+ lastHighlightedSearch: string;
40
+ internalLinkClicked: boolean;
41
+ }> {
42
+ @api apiDomain = "https://developer.salesforce.com";
43
+ @api hideFooter = false;
44
+
45
+ @api
46
+ get allLanguages(): Array<Language> {
47
+ return this._allLanguages;
48
+ }
49
+
50
+ set allLanguages(value: string) {
51
+ if (value) {
52
+ this._allLanguages = JSON.parse(value);
53
+ }
54
+ }
55
+
56
+ private availableLanguages: Array<DocLanguage> = [];
57
+ @track private availableVersions: Array<DocVersion> = [];
58
+ private contentProvider?: FetchContent;
59
+ private docContent = "";
60
+ private language?: DocLanguage | null = null;
61
+ private displayContent = false;
62
+ private display404 = false;
63
+ private _pageHeader?: Header;
64
+ private pdfUrl = "";
65
+ private tocMap: TocMap = {};
66
+ private sidebarContent: Array<TreeNode> | null = null;
67
+ private version: DocVersion | null = null;
68
+ private docTitle = "";
69
+ private _pathName = "";
70
+ private listenerAttached = false;
71
+ private sidebarFooterContent: SiderbarFooter = { ...defaultSidebarFooter };
72
+ private latestVersion = false;
73
+ private previewVersion = false;
74
+
75
+ private get enableFooter(): boolean {
76
+ return !this.hideFooter;
77
+ }
78
+
79
+ private searchSyncer = new SearchSyncer({
80
+ callbacks: {
81
+ onSearchChange: (nextSearchString: string): void => {
82
+ if (nextSearchString !== this.pageReference.search) {
83
+ this.updatePageReference({
84
+ ...this.pageReference,
85
+ search: nextSearchString
86
+ });
87
+ }
88
+ if (nextSearchString !== this.state.lastHighlightedSearch) {
89
+ this.updateHighlighting(
90
+ new URLSearchParams(nextSearchString).get("q") || ""
91
+ );
92
+ }
93
+ },
94
+ onUrlChange: (nextSearchString: string): void => {
95
+ if (nextSearchString !== this.pageReference.search) {
96
+ this.updatePageReference({
97
+ ...this.pageReference,
98
+ search: nextSearchString
99
+ });
100
+ }
101
+ const nextSearchParam =
102
+ new URLSearchParams(nextSearchString).get("q") || "";
103
+ this.updateSearchInput(nextSearchParam);
104
+ if (nextSearchString !== this.state.lastHighlightedSearch) {
105
+ this.updateHighlighting(nextSearchParam);
106
+ }
107
+ }
108
+ },
109
+ eventName: "sidebarsearchchange",
110
+ historyMethod: window.history.pushState,
111
+ searchParam: "q",
112
+ shouldStopPropagation: true,
113
+ target: window
114
+ });
115
+ private _allLanguages: Array<Language> = [];
116
+
117
+ private get oldVersionInfo(): DocPhaseInfo | null {
118
+ let info = null;
119
+ if (!this.disableVersion) {
120
+ const currentGAVersion = this.versionOptions.find(
121
+ (version) => !version.url.includes(version.id)
122
+ );
123
+ if (currentGAVersion?.link?.href && this.version?.id) {
124
+ const versionNo = currentGAVersion.id;
125
+ /**
126
+ * Need to show old version doc banner only if the version is less than the current ga version
127
+ * We should not show it to the preview version whose version is more than ga
128
+ **/
129
+ try {
130
+ if (parseFloat(this.version.id) < parseFloat(versionNo)) {
131
+ info = oldVersionDocInfo(currentGAVersion.link.href);
132
+ } else if (
133
+ parseFloat(this.version.id) > parseFloat(versionNo)
134
+ ) {
135
+ this.previewVersion = true;
136
+ }
137
+ } catch (exception) {
138
+ /* Ideally this use case should not happen, but just added to not to break the page*/
139
+ console.warn(exception);
140
+ }
141
+ }
142
+ }
143
+ return info;
144
+ }
145
+
146
+ @track showVersionBanner = false;
147
+
148
+ @track private pageReference: PageReference = {};
149
+ @track breadcrumbs: Array<Breadcrumb> = [];
150
+
151
+ constructor() {
152
+ super();
153
+ this.pageReference = this.getReferenceFromUrl();
154
+ // In order to prevent dispatching unnecessary highlight changes, we
155
+ // track these items and use their previous values for comparisons in
156
+ // `renderedCallback`:
157
+ this.state = {
158
+ isFetchingContent: false,
159
+ isFetchingDocument: false,
160
+ lastHighlightedSearch: "",
161
+ internalLinkClicked: false
162
+ };
163
+ }
164
+
165
+ connectedCallback(): void {
166
+ if (!this.pageReference?.deliverable) {
167
+ window.location.href = "/docs";
168
+ return;
169
+ }
170
+ this.contentProvider = new FetchContent(
171
+ this.apiDomain,
172
+ this.allLanguages
173
+ );
174
+ this.fetchDocument().then((content: any) => {
175
+ if (content) {
176
+ this.displayContent = true;
177
+ } else {
178
+ this.display404 = true;
179
+ }
180
+ });
181
+ window.addEventListener("popstate", this.handlePopState);
182
+
183
+ this.searchSyncer.init();
184
+ }
185
+
186
+ renderedCallback(): void {
187
+ this.setState({ internalLinkClicked: true });
188
+ const urlSectionLink =
189
+ this.pageReference?.hash?.split("#").length! > 1
190
+ ? this.pageReference.hash!.split("#")[1]
191
+ : this.pageReference?.hash;
192
+
193
+ const contentEl = this.template.querySelector("doc-content");
194
+ const anchorEl =
195
+ urlSectionLink &&
196
+ (contentEl?.shadowRoot?.querySelector(`[id='${urlSectionLink}']`) ||
197
+ contentEl?.shadowRoot?.querySelector(
198
+ `[name='${urlSectionLink}']`
199
+ ));
200
+
201
+ if (anchorEl) {
202
+ anchorEl.scrollIntoView();
203
+
204
+ this.setState({ internalLinkClicked: false });
205
+ }
206
+
207
+ if (
208
+ (this.prevState.isFetchingContent &&
209
+ !this.state.isFetchingContent) ||
210
+ (this.prevState.isFetchingDocument &&
211
+ !this.state.isFetchingDocument)
212
+ ) {
213
+ const prevSearchParam =
214
+ new URLSearchParams(this.state.lastHighlightedSearch).get(
215
+ "q"
216
+ ) || "";
217
+ const nextSearchParam =
218
+ new URLSearchParams(this.pageReference.search).get("q") || "";
219
+ this.updateHighlighting(nextSearchParam);
220
+ if (prevSearchParam !== nextSearchParam) {
221
+ this.updateSearchInput(nextSearchParam);
222
+ }
223
+ }
224
+ }
225
+
226
+ disconnectedCallback(): void {
227
+ window.removeEventListener("popstate", this.handlePopState);
228
+ this.searchSyncer.dispose();
229
+ }
230
+
231
+ private get languageId(): string | undefined {
232
+ return this.language?.id.replace("-", "_");
233
+ }
234
+
235
+ private get releaseVersionId(): string | undefined {
236
+ return this.version?.id;
237
+ }
238
+
239
+ private get deliverable(): string | undefined {
240
+ return this.pageReference.deliverable;
241
+ }
242
+
243
+ private get useOldSidebar(): boolean {
244
+ // Coveo is enabled and the version is greater than 51 (within the latest 3 versions)
245
+ // TODO: we need a better fix for version number check
246
+ return !(
247
+ !this.version?.releaseVersion ||
248
+ (this.version?.releaseVersion &&
249
+ parseInt(this.version.releaseVersion.replace("v", ""), 10) >=
250
+ 53)
251
+ );
252
+ }
253
+
254
+ private get pageHeader(): Header {
255
+ if (!this._pageHeader) {
256
+ this._pageHeader = document.querySelector("doc-header")!;
257
+ }
258
+
259
+ return this._pageHeader;
260
+ }
261
+
262
+ private get sidebarValue(): string {
263
+ if (this.pageReference?.contentDocumentId) {
264
+ const hashedUri = `${
265
+ this.pageReference.contentDocumentId
266
+ }${this.normalizeHash(this.pageReference.hash)}`;
267
+ if (hashedUri in this.tocMap) {
268
+ return hashedUri;
269
+ }
270
+
271
+ if (this.pageReference.contentDocumentId in this.tocMap) {
272
+ return this.pageReference.contentDocumentId;
273
+ }
274
+ }
275
+
276
+ return "";
277
+ }
278
+
279
+ private get disableVersion(): boolean {
280
+ return !this.availableVersions || this.availableVersions.length <= 1;
281
+ }
282
+
283
+ private get versionOptions(): Array<DocVersion> {
284
+ return this.disableVersion
285
+ ? this.availableVersions
286
+ : this.availableVersions.map((version) => ({
287
+ ...version,
288
+ link: {
289
+ href: this.pageReferenceToString({
290
+ ...this.pageReference,
291
+ docId: version.url
292
+ })
293
+ }
294
+ }));
295
+ }
296
+
297
+ private get breadcrumbPixelPerCharacter() {
298
+ return (
299
+ PIXEL_PER_CHARACTER_MAP[this.language!.id] ||
300
+ PIXEL_PER_CHARACTER_MAP.default
301
+ );
302
+ }
303
+
304
+ private get ANALYTICS_PAYLOAD() {
305
+ return {
306
+ element_title: "version picker",
307
+ content_category: "cta"
308
+ };
309
+ }
310
+
311
+ private handlePopState = (event: PopStateEvent): void =>
312
+ this.updatePageReference(this.getReferenceFromUrl(), event);
313
+
314
+ handleDismissVersionBanner() {
315
+ this.showVersionBanner = false;
316
+ }
317
+
318
+ handleSelect(event: CustomEvent<{ name: string }>): void {
319
+ event.stopPropagation();
320
+ const { name } = event.detail;
321
+
322
+ if (this.sidebarValue === name) {
323
+ return;
324
+ }
325
+
326
+ if (name) {
327
+ const hashIndex = name.indexOf("#");
328
+ const hash = hashIndex > -1 ? name.slice(hashIndex) : "";
329
+
330
+ const contentDocumentId =
331
+ hashIndex > -1 ? name.slice(0, hashIndex) : name;
332
+ this.updatePageReference({
333
+ ...this.pageReference,
334
+ contentDocumentId,
335
+ hash
336
+ });
337
+ this.updateUrl();
338
+ }
339
+ }
340
+
341
+ handleNavClick(event: CustomEvent<{ pageReference: PageReference }>): void {
342
+ event.stopPropagation();
343
+ const { pageReference } = event.detail;
344
+ this.updatePageReference(pageReference);
345
+ this.updateUrl();
346
+ }
347
+
348
+ handleLanguageChange = (event: any) => {
349
+ if (this.language && this.language.id === event.detail) {
350
+ return;
351
+ }
352
+
353
+ this.language = this.availableLanguages.find(
354
+ ({ id }) => id === event.detail
355
+ );
356
+ this.pageReference.docId = this.language!.url;
357
+
358
+ trackGTM(event.target!, "custEv_ctaLinkClick", {
359
+ click_text: event.detail,
360
+ element_title: "language selector",
361
+ click_url: `${window.location.origin}${this.pageReferenceToString(
362
+ this.pageReference
363
+ )}`,
364
+ element_type: "link",
365
+ content_category: "cta"
366
+ });
367
+
368
+ this.updateUrl();
369
+ this.fetchDocument();
370
+ };
371
+
372
+ updatePageReference(
373
+ newPageReference: PageReference,
374
+ event: PopStateEvent | undefined = undefined
375
+ ): void {
376
+ this.pageReference.hash = newPageReference.hash;
377
+ this.pageReference.search = newPageReference.search;
378
+
379
+ if (this.isSamePage(newPageReference)) {
380
+ return;
381
+ }
382
+
383
+ const isSameDocId = this.pageReference.docId === newPageReference.docId;
384
+ this.pageReference = newPageReference;
385
+
386
+ if (!isSameDocId) {
387
+ this.fetchDocument();
388
+ return;
389
+ }
390
+
391
+ this.fetchContent()
392
+ .then(() => {
393
+ this.buildBreadcrumbs();
394
+ document.body.scrollTop = event?.state?.scroll?.value || 0;
395
+ })
396
+ .catch(handleContentError);
397
+ }
398
+
399
+ private sanitizeUrlPart(part: string | undefined): string | undefined {
400
+ if (!part) {
401
+ return part;
402
+ }
403
+ return DOMPurify.sanitize(part);
404
+ }
405
+
406
+ getReferenceFromUrl(): PageReference {
407
+ const [page, docId, deliverable, contentDocumentId] =
408
+ window.location.pathname
409
+ .substr(1)
410
+ .split("/")
411
+ .map(this.sanitizeUrlPart);
412
+
413
+ const { origin: domain, hash, search } = window.location;
414
+
415
+ return {
416
+ contentDocumentId,
417
+ deliverable,
418
+ docId,
419
+ domain,
420
+ hash: this.sanitizeUrlPart(hash),
421
+ page,
422
+ search: this.sanitizeUrlPart(search)
423
+ };
424
+ }
425
+
426
+ isSamePage(reference: PageReference): boolean {
427
+ return (
428
+ this.pageReference.contentDocumentId ===
429
+ reference.contentDocumentId &&
430
+ this.pageReference.docId === reference.docId &&
431
+ this.pageReference.page === reference.page &&
432
+ this.pageReference.deliverable === reference.deliverable
433
+ );
434
+ }
435
+
436
+ async fetchDocument(): Promise<string> {
437
+ this.setState({
438
+ isFetchingDocument: true
439
+ });
440
+
441
+ const data = await this.contentProvider!.fetchDocumentData(
442
+ this.pageReference.docId!
443
+ );
444
+
445
+ // This could be a 404 scenario.
446
+ if (!data) {
447
+ this.setState({
448
+ isFetchingDocument: false
449
+ });
450
+ return "";
451
+ }
452
+
453
+ this.docTitle = data.docTitle;
454
+ this.tocMap = data.tocMap;
455
+ this.sidebarContent = data.toc;
456
+ this.version = data.version;
457
+ this.language = data.language;
458
+ this.availableLanguages = data.availableLanguages;
459
+ this.availableVersions = data.availableVersions;
460
+ this.pdfUrl = data.pdfUrl;
461
+
462
+ this.updateHeaderAndSidebarFooter();
463
+
464
+ this.buildBreadcrumbs();
465
+
466
+ if (this.pageReference.deliverable !== data.deliverable) {
467
+ this.pageReference.deliverable = data.deliverable;
468
+ this.updateUrl(HistoryState.REPLACE_STATE);
469
+ }
470
+
471
+ if (this.oldVersionInfo) {
472
+ this.showVersionBanner = true;
473
+ } else {
474
+ this.latestVersion = true;
475
+ }
476
+
477
+ if (
478
+ this.pageReference?.contentDocumentId?.replace(/\.htm$/, "") !==
479
+ data.id
480
+ ) {
481
+ try {
482
+ const moreData = await this.fetchContent();
483
+ this.setState({
484
+ isFetchingDocument: false
485
+ });
486
+ return moreData.content;
487
+ } catch (error) {
488
+ this.pageReference.contentDocumentId = `${data.id}.htm`;
489
+ this.pageReference.hash = "";
490
+ this.pageReference.search = "";
491
+ this.updateUrl(HistoryState.REPLACE_STATE);
492
+ }
493
+ }
494
+
495
+ this.docContent = data.content;
496
+ this.addMetatags();
497
+ this.setState({
498
+ isFetchingDocument: false
499
+ });
500
+ return data.content;
501
+ }
502
+
503
+ async fetchContent(): Promise<ContentData> {
504
+ this.setState({
505
+ isFetchingContent: true
506
+ });
507
+ const data = await this.contentProvider!.fetchContent(
508
+ this.pageReference.deliverable!,
509
+ this.pageReference.contentDocumentId!,
510
+ {
511
+ language: this.language!.id,
512
+ version: this.version!.id
513
+ }
514
+ );
515
+
516
+ if (data) {
517
+ this.docContent = data.content;
518
+ this.addMetatags();
519
+
520
+ if (!this.pageReference.hash) {
521
+ document.body.scrollIntoView();
522
+ }
523
+ }
524
+ this.setState({
525
+ isFetchingContent: false
526
+ });
527
+ return data;
528
+ }
529
+
530
+ updateHeaderAndSidebarFooter(): void {
531
+ if (!this.pageHeader) {
532
+ return;
533
+ }
534
+
535
+ if (this.docTitle) {
536
+ this.pageHeader.subtitle = this.docTitle;
537
+ }
538
+
539
+ if (this.pdfUrl) {
540
+ this.sidebarFooterContent.bailHref = this.pdfUrl;
541
+ this.sidebarFooterContent.bailLabel = "PDF";
542
+ }
543
+
544
+ this.sidebarFooterContent.languages = this.availableLanguages;
545
+ this.sidebarFooterContent.language = this.language?.id;
546
+
547
+ if (this.pageReference) {
548
+ const { docId, deliverable, page } = this.pageReference;
549
+ this.pageHeader.headerHref = `/${page}/${docId}/${deliverable}/`;
550
+ }
551
+ }
552
+
553
+ updateUrl(method = HistoryState.PUSH_STATE): void {
554
+ window.history[method](
555
+ {},
556
+ "docs",
557
+ this.pageReferenceToString(this.pageReference)
558
+ );
559
+ }
560
+
561
+ private updateHighlighting(searchParam: string): void {
562
+ this.dispatchHighlightChange(searchParam);
563
+ this.setState({
564
+ lastHighlightedSearch: this.pageReference.search
565
+ });
566
+ }
567
+
568
+ private updateSearchInput(searchParam: string): void {
569
+ (this.refs.docContentLayout as any)?.setSidebarInputValue(searchParam);
570
+ }
571
+
572
+ private pageReferenceToString(reference: PageReference): string {
573
+ const { page, docId, deliverable, contentDocumentId, hash, search } =
574
+ reference;
575
+ return `/${page}/${docId}/${deliverable}/${contentDocumentId}${this.normalizeSearch(
576
+ search!
577
+ )}${this.normalizeHash(hash)}`;
578
+ }
579
+
580
+ private normalizeUrlPart(
581
+ part: string | undefined,
582
+ sentinel: string
583
+ ): string {
584
+ return (
585
+ (part &&
586
+ (part.startsWith(sentinel!) ? part : `${sentinel}${part}`)) ||
587
+ ""
588
+ );
589
+ }
590
+
591
+ private normalizeSearch(search: string): string {
592
+ return this.normalizeUrlPart(search, "?");
593
+ }
594
+
595
+ private normalizeHash(hash?: string): string {
596
+ return this.normalizeUrlPart(hash, "#");
597
+ }
598
+
599
+ private getComposedTitle(
600
+ topicTitle: string | null | undefined,
601
+ docTitle: string | undefined
602
+ ): string {
603
+ // map to avoid duplicates
604
+ const titleMap: { [key: string]: any } = {};
605
+ if (topicTitle) {
606
+ // sometimes the h1 tag text (which is docSubTitle) contains text with new line character. For e.g, "Bulk API 2.0 Older\n Documentation",
607
+ // here it contains \n in the text context which needs to be removed
608
+ // also, there are multiple spaces in between the text, which needs to be replaced with a single space
609
+ const docTopicTitle = topicTitle
610
+ .replace(/[\t\r\n]/g, " ")
611
+ .replace(/ +/g, " ")
612
+ .trim();
613
+ titleMap[docTopicTitle] = true;
614
+ }
615
+
616
+ if (docTitle) {
617
+ titleMap[docTitle] = true;
618
+ }
619
+
620
+ titleMap["Salesforce Developers"] = true;
621
+
622
+ return Object.keys(titleMap).join(" | ");
623
+ }
624
+
625
+ private dispatchHighlightChange(term: string): void {
626
+ this.dispatchEvent(
627
+ new CustomEvent("highlightedtermchange", {
628
+ detail: term,
629
+ bubbles: true,
630
+ composed: true
631
+ })
632
+ );
633
+ }
634
+
635
+ get showBreadcrumbs(): boolean {
636
+ return this.breadcrumbs && this.breadcrumbs.length > 1;
637
+ }
638
+
639
+ private buildBreadcrumbs(): void {
640
+ const { contentDocumentId } = this.pageReference;
641
+ if (!contentDocumentId) {
642
+ return;
643
+ }
644
+
645
+ const currentNode = this.tocMap[contentDocumentId];
646
+
647
+ if (currentNode?.parent) {
648
+ this.breadcrumbs = this.nodeToBreadcrumb(currentNode);
649
+ } else {
650
+ this.breadcrumbs = [];
651
+ }
652
+ }
653
+
654
+ private nodeToBreadcrumb(node: TreeNode): Breadcrumb[] {
655
+ const item = {
656
+ href: this.pageReferenceToString({
657
+ ...this.pageReference,
658
+ contentDocumentId: node.name
659
+ }),
660
+ label: node.label
661
+ };
662
+
663
+ if (node.parent) {
664
+ return [...this.nodeToBreadcrumb(node.parent), item];
665
+ }
666
+
667
+ return [item];
668
+ }
669
+
670
+ // This method take docId and drops the version from the docId.
671
+ // Example:
672
+ // Takes input string: docId = "atlas.en-us.238.0.b2b_b2c_comm_dev.meta"
673
+ // Output string: filteredDocId = "atlas.en-us.b2b_b2c_comm_dev.meta"
674
+ dropVersionFromDocId(docId: string): string {
675
+ if (!this.version?.id) {
676
+ return docId;
677
+ }
678
+
679
+ const curVersion = this.version.id + ".";
680
+ const filteredDocId = docId.replace(curVersion, "");
681
+ return filteredDocId;
682
+ }
683
+
684
+ addMetatags(): void {
685
+ const div = document.createElement("div");
686
+ div.innerHTML = DOMPurify.sanitize(this.docContent);
687
+ const docDescription = div.querySelector(".shortdesc")?.textContent;
688
+ const topicTitle = div.querySelector("h1")?.textContent;
689
+
690
+ const title = document.querySelector("title");
691
+ const composedTitle = this.getComposedTitle(topicTitle, this.docTitle);
692
+
693
+ if (title && title.textContent) {
694
+ title.textContent = composedTitle;
695
+ }
696
+ const metatitle = document.querySelector('meta[name="title"]');
697
+ if (metatitle) {
698
+ metatitle.setAttribute("content", composedTitle);
699
+ }
700
+
701
+ if (docDescription) {
702
+ const metadescription = document.querySelector(
703
+ 'meta[name="description"]'
704
+ );
705
+ if (metadescription) {
706
+ metadescription.setAttribute("content", docDescription);
707
+ }
708
+ }
709
+
710
+ if (this.pageReference) {
711
+ const metadescription = document.querySelector(
712
+ 'link[rel="canonical"]'
713
+ );
714
+ if (metadescription) {
715
+ const copyPageReference = { ...this.pageReference };
716
+ copyPageReference.docId = copyPageReference.docId
717
+ ? this.dropVersionFromDocId(copyPageReference.docId)
718
+ : copyPageReference.docId;
719
+ metadescription.setAttribute(
720
+ "href",
721
+ window.location.protocol +
722
+ "//" +
723
+ window.location.host +
724
+ this.pageReferenceToString(copyPageReference)
725
+ );
726
+ }
727
+ }
728
+
729
+ this.addNoIndexMetaForOlderDocVersions();
730
+ }
731
+
732
+ /**
733
+ * Method adds noindex, follow meta tag to the older Couch DB doc pages.
734
+ * Fixes W-12547462.
735
+ */
736
+ private addNoIndexMetaForOlderDocVersions() {
737
+ // eslint-disable-next-line @lwc/lwc/no-document-query
738
+ const headTag = document.getElementsByTagName("head");
739
+ // this checks if the selected version is not the latest version,
740
+ // then it adds the noindex, follow meta tag to the older version pages.
741
+ const versionId = this.version!.id;
742
+ const docId = this.pageReference.docId;
743
+
744
+ // SEO fix:
745
+ // Doc id without version id is always considered latest and should be used for SEO.
746
+ // Condition is to find a docId which includes version id,
747
+ // these docs are always considered as old and should not be indexed including the preview docs.
748
+ if (
749
+ headTag.length &&
750
+ docId?.includes(versionId) &&
751
+ !document.querySelector('meta[name="robots"]')
752
+ ) {
753
+ const robotsMeta = document.createElement("meta");
754
+ robotsMeta.setAttribute("name", "robots");
755
+ robotsMeta.setAttribute("content", "noindex, follow");
756
+ headTag[0].appendChild(robotsMeta);
757
+ }
758
+ }
759
+
760
+ private get showVersionPicker(): boolean {
761
+ return !this.disableVersion;
762
+ }
763
+ }