This repository has been archived by the owner on Sep 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
config.js
230 lines (220 loc) · 7.57 KB
/
config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* Copyright 2021 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
// This file contains the blog-specific configuration for the sidekick.
(() => {
function getArticleData(sk) {
let date = 0;
if (window.blog.rawDate) {
const [month, day, year] = window.blog.rawDate.split('-');
date = Date.UTC(parseInt(year), parseInt(month) - 1, parseInt(day), 15); // pacific time AM
}
let hero = '';
const metaImg = document.head.querySelector('meta[property="og:image"]');
if (metaImg) {
hero = new URL(metaImg.getAttribute('content')).pathname;
}
return [
window.blog.author,
date/1000,
hero,
predictUrl(null, sk.location.pathname),
`["${window.blog.products.join('\", \"')}"]`,
'0',
document.querySelector('main>div:nth-of-type(4)').textContent.trim().substring(0, 75),
document.title,
`["${window.blog.topics.join('\", \"')}"]`,
];
}
function getCardData(sk) {
const d = getArticleData(sk);
return {
author: d[0],
date: d[1],
hero: d[2],
path: window.location.pathname.substring(1),
teaser: d[6],
title: d[7],
topics: [...window.blog.allVisibleTopics],
};
}
async function toggleCardPreview(sk) {
if (document.getElementById('hlx-sk-card-preview')) {
document.getElementById('hlx-sk-card-preview').remove();
document.body.removeEventListener('keydown', cardPreviewEscListener);
} else {
const {
addCard,
itemTransformer,
} = await import('/scripts/v2/common.js');
const $overlay = document.createElement('div');
$overlay.id = 'hlx-sk-card-preview';
$overlay.addEventListener('click', () => {
toggleCardPreview(sk);
});
document.body.prepend($overlay);
const $modal = document.createElement('div');
$modal.innerHTML = `
<style>
#hlx-sk-card-preview {
z-index: 9999998;
position: fixed;
width: 100vw;
height: 100vh;
top: 0;
left: 0;
background-color: rgba(0,0,0, 0.5);
display: flex;
align-items: center;
justify-content: center;
}
#hlx-sk-card-preview .card {
width: 376px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.1), 0 10px 20px 0 rgba(0, 0, 0, 0.3);
}
</style>`;
addCard(await itemTransformer(getCardData(sk)), $modal);
$overlay.append($modal);
document.body.addEventListener('keydown', cardPreviewEscListener);
}
sk.get('card-preview').firstElementChild.classList.toggle('pressed');
}
function cardPreviewEscListener(keyEvt) {
if (keyEvt.key === 'Escape') {
toggleCardPreview(sk);
}
};
function allowedPostPath(path) {
return ![
'documentation',
'fpost',
].includes(path.split('/')[2]);
}
function predictUrl(host, path) {
const pathsplits = path.split('/');
let publishPath = '';
if (window.blog && window.blog.rawDate) {
const datesplits = window.blog.rawDate.split('-');
if (datesplits.length > 2) {
publishPath = `/publish/${datesplits[2]}/${datesplits[0]}/${datesplits[1]}`;
}
}
const filename = (pathsplits[pathsplits.length-1].split('.')[0]).toLowerCase().replace(/[^a-z\d_\/\.]/g,'-');
return `${host ? `https://${host}/` : ''}${pathsplits[1]}${publishPath}/${filename}.html`;
}
const sk = window.hlx.initSidekick({
project: 'Adobe Blog',
host: 'blog.adobe.com',
pushDownSelector: '#feds-header',
plugins: [
// TAGGER -----------------------------------------------------------------------
{
id: 'tagger',
condition: (sk) => sk.isEditor() && (sk.location.search.includes('.docx&') || sk.location.search.includes('.md&')),
button: {
text: 'Tagger',
action: (_, sk) => {
const { config } = sk;
window.open(`https://${config.host}/tools/tagger/`, 'hlx-sidekick-tagger');
},
},
},
// CARD PREVIEW -------------------------------------------------------------------
{
id: 'card-preview',
condition: (sidekick) => {
return sidekick.isHelix()
&& window.blog && window.blog.pageType === window.blog.TYPE.POST
&& allowedPostPath(sidekick.location.pathname);
},
button: {
text: 'Card Preview',
action: () => toggleCardPreview(sk),
},
},
// PREDICTED URL ----------------------------------------------------------------
{
id: 'predicted-url',
condition: (sidekick) => {
const { config, location } = sidekick;
return sidekick.isHelix()
&& window.blog && window.blog.pageType === window.blog.TYPE.POST
&& config.host
&& location.host != config.host
&& allowedPostPath(location.pathname);
},
button: {
text: 'Copy Predicted URL',
action: (evt) => {
const { config, location } = sk;
const url = predictUrl(config.host, location.pathname);
navigator.clipboard.writeText(url);
sk.notify([
'Predicted URL copied to clipboard:',
url,
]);
},
},
},
// ARTICLE DATA -------------------------------------------------------------------
{
id: 'article-data',
condition: (sidekick) => {
return sidekick.isHelix()
&& window.blog && window.blog.pageType === window.blog.TYPE.POST;
},
button: {
text: 'Copy Article Data',
action: async (_, sk) => {
try {
navigator.clipboard.writeText(getArticleData(sk).join('\t'));
sk.notify('Article data copied to clipboard');
} catch (e) {
sk.notify([
'Unable to copy article data:',
e,
], 0);
}
},
},
},
// PUBLISH ----------------------------------------------------------------------
{
id: 'publish',
condition: (sidekick) => {
// do not show publish button for drafts
return sidekick.isHelix() && !sidekick.location.pathname.includes('/drafts/');
},
},
],
});
const path = sk.location.pathname;
if (!path.includes('/publish/') && /\d{4}\/\d{2}\/\d{2}/.test(path)) {
// post URL without publish in the path, add it back
const segs = path.split('/');
segs.splice(2, 0, 'publish')
sk.location = new URL(segs.join('/'), sk.location.origin);
}
sk.addEventListener('statusfetched', () => {
const webPath = sk.status.webPath;
if (!webPath) {
return;
}
const file = webPath.split('/').pop();
if (file && !file.includes('.')) {
// add .html extension back
sk.status.webPath = `${webPath}.html`;
} else if (webPath.endsWith('taxonomy.json')) {
// add leading underscore back
sk.status.webPath = webPath.replace('taxonomy.json', '_taxonomy.json');
}
});
})();