-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.js
More file actions
60 lines (53 loc) · 2.11 KB
/
Copy pathformat.js
File metadata and controls
60 lines (53 loc) · 2.11 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
// format.js — shared note → markdown formatting.
// Loaded both as a content script (before content.js) and by the side panel,
// so the in-page "Copy" button and the sidebar produce identical output.
(() => {
if (window.__caFormat) return;
const PROP_RULES = [
['fontSize', v => `font-size: ${v.startsWith('var(') ? v : v + 'px'}`],
['fontWeight', v => `font-weight: ${v}`],
['color', v => `color: ${v}`],
['backgroundColor', v => `background: ${v}`],
['borderRadius', v => `border-radius: ${v}px`],
['flexDirection', v => `flex-direction: ${v}`],
['alignItems', v => `align-items: ${v}`],
['justifyContent', v => `justify-content: ${v}`],
['gap', v => `gap: ${v}px`],
];
function propsToCSS(props) {
if (!props) return '';
return PROP_RULES.filter(([k]) => props[k]).map(([k, fn]) => fn(props[k])).join('; ');
}
// One markdown list item per note, matching the in-page toolbar's format.
function formatNoteItem(n, idx) {
const extra = [];
const css = propsToCSS(n.props);
if (css) extra.push(css);
extra.push(...(n.note || '').split('\n').filter(Boolean));
if (extra.length === 0) return `${idx}. ${n.html}`;
if (extra.length === 1 && !css) return `${idx}. ${n.html}: ${extra[0]}`;
return `${idx}. ${n.html}\n${extra.map(l => ` ${l}`).join('\n')}`;
}
function notesToMarkdown(notes) {
return (notes || []).map((n, i) => formatNoteItem(n, i + 1)).join('\n');
}
// Every page that still has notes, each under its own heading.
function allNotesToMarkdown(byPage) {
return Object.entries(byPage || {})
.filter(([, notes]) => notes && notes.length)
.map(([page, notes]) => `## ${page}\n\n${notesToMarkdown(notes)}`)
.join('\n\n');
}
// Fallback label for notes saved before we started storing `name`.
function nameFromHtml(html) {
const m = /^<\s*([a-zA-Z][\w-]*)/.exec(html || '');
return m ? m[1].toLowerCase() : 'element';
}
window.__caFormat = {
propsToCSS,
formatNoteItem,
notesToMarkdown,
allNotesToMarkdown,
nameFromHtml,
};
})();