-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpublish.js
More file actions
321 lines (288 loc) · 11.7 KB
/
publish.js
File metadata and controls
321 lines (288 loc) · 11.7 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//Posthog
!function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.crossOrigin="anonymous",p.async=!0,p.src=s.api_host.replace(".i.posthog.com","-assets.i.posthog.com")+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="init capture register register_once register_for_session unregister unregister_for_session getFeatureFlag getFeatureFlagPayload isFeatureEnabled reloadFeatureFlags updateEarlyAccessFeatureEnrollment getEarlyAccessFeatures on onFeatureFlags onSessionId getSurveys getActiveMatchingSurveys renderSurvey canRenderSurvey identify setPersonProperties group resetGroups setPersonPropertiesForFlags resetPersonPropertiesForFlags setGroupPropertiesForFlags resetGroupPropertiesForFlags reset get_distinct_id getGroups get_session_id get_session_replay_url alias set_config startSessionRecording stopSessionRecording sessionRecordingStarted captureException loadToolbar get_property getSessionProperty createPersonProfile opt_in_capturing opt_out_capturing has_opted_in_capturing has_opted_out_capturing clear_opt_in_out_capturing debug getPageViewId captureTraceFeedback captureTraceMetric".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]);
posthog.init('phc_599knGAT15HiAYE5nHJjTqIlAY3O6e2F3QdlFzIRHKb', {
api_host: 'https://us.i.posthog.com',
person_profiles: 'identified_only', // or 'always' to create profiles for anonymous users as well
})
/* =========================================================================
publish.js – Minimal setInterval approach to insert top links + footer block
========================================================================= */
console.log("[publish.js] loaded");
// A small ID for our repeating check
let insertIntervalId;
const welcomePage = "README";
const USE_PROPERTY_TABLE = true; // If false, modifies existing .frontmatter HTML
/**
* Determines if the current screen width qualifies as mobile (≤ 750px).
*/
function isMobile() {
return window.matchMedia("(max-width: 750px)").matches;
}
/**
* buildGitHubURLs():
* Build final .md links from current URL path, replacing "+" with "%20".
*/
function buildGitHubURLs() {
const currentUrlPath = decodeURIComponent(window.location.pathname);
const relativePath = currentUrlPath.replace(/^\/|\/$/g, "");
const githubUser = "cybersader";
const githubRepo = "awesome-cybersader";
const githubBranch = "main";
// Convert "+" => "%20"
const encodedPath = relativePath.replace(/\+/g, "%20");
const view = `https://github.com/${githubUser}/${githubRepo}/blob/${githubBranch}/${encodedPath}.md`;
const edit = view.replace("/blob/", "/edit/");
const raw = `https://raw.githubusercontent.com/${githubUser}/${githubRepo}/${githubBranch}/${encodedPath}.md`;
const dev = `https://github.dev/${githubUser}/${githubRepo}/blob/${githubBranch}/${encodedPath}.md`;
const download = `https://github.com/${githubUser}/${githubRepo}/archive/refs/heads/${githubBranch}.zip`;
return { view, edit, raw, dev, download };
}
/**
* insertHeaderLinksIfMissing():
* Inserts different sets of links based on screen size.
*/
function insertHeaderLinksIfMissing() {
const existingHeaderBlock = document.getElementById("header-git-links");
if (existingHeaderBlock) {
return; // Already inserted
}
const headerEl = document.querySelector(".page-header");
if (!headerEl) {
return; // Header not rendered yet
}
// Build URLs
const { view, edit, raw, download } = buildGitHubURLs();
// Determine which links to show
let html;
if (isMobile()) {
// Only show first two links on mobile
html = `
<div id="header-git-links" class="header-git-links">
<a href="${view}" target="_blank">View Source</a> |
<a href="${edit}" target="_blank">Edit Page</a>
</div>
`;
} else {
// Show all links on larger screens
html = `
<div id="header-git-links" class="header-git-links">
<a href="${view}" target="_blank">View Source</a> |
<a href="${edit}" target="_blank">Edit Page</a> |
<a href="${raw}" target="_blank">Copy</a> |
<a href="${download}" target="_blank">Download Vault</a>
</div>
`;
}
// Insert the links
headerEl.insertAdjacentHTML("afterend", html);
console.log("[GitHub Links] Inserted #header-git-links.");
}
/**
* insertFooterBlockIfMissing():
* - If #footer-action-block doesn't exist and .mod-footer.mod-ui does, insert
* an <hr> + "Interact with this page" heading + 4 button-like links.
*/
function insertFooterBlockIfMissing() {
const existingFooterBlock = document.getElementById("footer-action-block");
if (existingFooterBlock) {
return; // Already inserted
}
const footerEl = document.querySelector(".mod-footer.mod-ui");
if (!footerEl) {
return; // Footer not rendered yet
}
// Build the block
const { edit, raw, download, dev } = buildGitHubURLs();
const html = `
<div id="footer-action-block">
<hr class="footer-divider" />
<div class="footer-action-container">
<div class="published-section-header">
<h4>Interact with this note</h4>
</div>
<div class="footer-buttons">
<a class="btn" title="git-hub-edit-note" href="${edit}" target="_blank">Edit Note</a>
<a class="btn" title="git-hub-copy-note" href="${raw}" target="_blank">Raw Note</a>
<a class="btn" title="git-hub-download-vault" href="${download}" target="_blank">Download Vault</a>
<a class="btn" title="git-hub-open-dev" href="${dev}" target="_blank">GitHub.dev Editor</a>
</div>
</div>
</div>
`;
footerEl.insertAdjacentHTML("beforeend", html);
console.log("[GitHub Links] Inserted #footer-action-block in .mod-footer.");
}
/**
* getFrontmatter():
* Pull frontmatter from Obsidian Publish's in-memory cache, if available.
*/
function getFrontmatter() {
try {
const path = decodeURIComponent(window.location.pathname).replace(/\+/g, " ");
const mdFile = path.substring(1) + ".md"; // e.g. "README.md"
if (app?.site?.cache?.cache[mdFile]?.frontmatter) {
return app.site.cache.cache[mdFile].frontmatter;
}
} catch (err) {
console.warn("[publish.js] frontmatter not found:", err);
}
return null;
}
/**
* frontmatterWhitelist:
* An array of objects describing which keys we want to display,
* how we label them, and how we transform the value if needed.
*
* key: the actual frontmatter key
* label: the displayed label (e.g. "📅 created")
* transform: a function(value) => string, or null to leave as-is
*/
const frontmatterWhitelist = [
{
key: "date created",
label: "📅 created",
transform: null
},
{
key: "date modified",
label: "📅 updated",
transform: null
}/*,
{
key: "tags",
label: "Tags",
// If it's an array, we join with commas
transform: (value) => Array.isArray(value) ? value.join(", ") : value
},
{
key: "aliases",
label: "Aliases",
transform: (value) => Array.isArray(value) ? value.join(", ") : value
}*/
// ... add more items if you wish
];
/**
* insertFrontmatterIfMissing():
* Checks for .frontmatter element + frontmatter data in cache.
* If #frontmatter-block not present, build a block with the whitelisted keys.
*/
function insertFrontmatterIfMissing() {
// If we already inserted #frontmatter-block (for table mode), skip
if (USE_PROPERTY_TABLE && document.getElementById("frontmatter-block")) {
return;
}
const fmElement = document.querySelector(".frontmatter");
if (!fmElement) return; // no frontmatter in DOM
const fmData = getFrontmatter();
if (!fmData) return; // none in cache
// Collect the whitelisted keys
let rows = "";
frontmatterWhitelist.forEach(({ key, label, transform }) => {
if (fmData[key]) {
let val = fmData[key];
if (typeof transform === "function") {
val = transform(val);
}
rows += `
<div class="propertyitem">
<strong>${label}:</strong> ${val}
</div>
`;
}
});
if (!rows.trim()) return;
if (USE_PROPERTY_TABLE) {
// === Option A: Insert a new #frontmatter-block after .frontmatter ===
if (document.getElementById("frontmatter-block")) return; // skip duplicates
const html = `
<div id="frontmatter-block" class="propertyitemtable">
${rows}
</div>
`;
fmElement.insertAdjacentHTML("afterend", html);
const originalYaml = document.querySelector(".frontmatter.language-yaml");
originalYaml.remove();
} else {
//TODO
}
}
/**
* removeEntireLine(span):
* Removes the DOM nodes from 'span' until the next newline or <br>.
* This effectively hides the entire line for a non-whitelisted key.
*/
function removeEntireLine(span) {
const parent = span.parentNode;
if (!parent) return;
// We'll remove siblings up to the next <br> or end-of-line
let current = span;
while (current && current.nodeName.toLowerCase() !== "br") {
const next = current.nextSibling;
parent.removeChild(current);
current = next;
}
// Optionally remove the <br> as well if you want that line entirely gone
if (current && current.nodeName.toLowerCase() === "br") {
parent.removeChild(current);
}
}
// Function to remove frontmatter for when we're on the welcome page
function removeFrontmatter() {
const frontmatter = document.querySelector(".el-pre.mod-frontmatter.mod-ui");
frontmatter.style.display = "none"; // Hides the element
}
// Function to remove page title for home page
function removeHeader() {
const header = document.querySelector(".mod-header.mod-ui")
header.style.display = "none"; // Hides the element
}
// Function to remove frontmatter for when we're on the welcome page
function showFrontmatter() {
const frontmatter = document.querySelector(".el-pre.mod-frontmatter.mod-ui");
frontmatter.style.display = "block";
}
// Function to remove page title for home page
function showHeader() {
const header = document.querySelector(".mod-header.mod-ui")
header.style.display = "block";
}
/**
* checkAndInsert():
* Called repeatedly by setInterval.
* If a block is missing, re-insert it.
*/
function checkAndInsert() {
if (decodeURIComponent(window.location.pathname).replace(/^\/|\/$/g, "") == welcomePage) {
removeFrontmatter();
removeHeader();
document.querySelector(".header-git-links").style.display = "none";
} else {
insertHeaderLinksIfMissing();
insertFooterBlockIfMissing();
insertFrontmatterIfMissing(); // Show whitelisted frontmatter
showFrontmatter();
showHeader();
// You could also do insertRightColumnEditButtonIfMissing() here if you want
}
}
/**
* init():
* Start a setInterval that calls checkAndInsert() every 100ms.
* If you want to automatically stop it once inserted, see note below.
*/
function init() {
console.log("[GitHub Links] init() called.");
// Start the repeating interval
// We can store the ID if we want to stop it after success, but often
// it's simplest to keep it running so if the user navigates or Publish re-renders,
// the blocks get re-inserted if needed.
insertIntervalId = setInterval(checkAndInsert, 100);
// Kick off an immediate check so there's no 100ms delay initially
checkAndInsert();
}
// Fire init once DOM is loaded
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init);
} else {
init();
}