@chriswiegman/hugo-tools 0.2.0 → 0.2.2
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.
- package/CHANGELOG.md +18 -0
- package/README.md +12 -1
- package/package.json +6 -5
- package/src/add-tags.js +103 -100
- package/src/book.mjs +643 -542
- package/src/book.test.mjs +660 -566
- package/src/config-init.js +9 -9
- package/src/config.js +22 -20
- package/src/draft.js +44 -42
- package/src/draft.test.mjs +32 -27
- package/src/extract-tags.js +184 -173
- package/src/extract-tags.test.mjs +56 -43
- package/src/pick-image.js +111 -97
- package/src/pick-image.test.mjs +57 -46
- package/src/publish.js +363 -317
- package/src/publish.test.mjs +242 -201
- package/src/vscode.js +68 -65
package/src/pick-image.js
CHANGED
|
@@ -13,115 +13,129 @@ const config = require('./config');
|
|
|
13
13
|
const IMAGE_EXTS = new Set(['.jpg', '.jpeg', '.png', '.webp', '.gif']);
|
|
14
14
|
|
|
15
15
|
function urlFromImagesDir(imagesDir, year, month, filename) {
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
const urlBase = imagesDir.replace(/^assets\//, '');
|
|
17
|
+
|
|
18
|
+
return `/${urlBase}/${year}/${month}/${filename}`;
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
function appendMarkdown(content, urlPath) {
|
|
21
|
-
|
|
22
|
+
return `${content.trimEnd()}\n\n\n`;
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
function insertImage(content, urlPath) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
26
|
+
const fmEnd = content.indexOf('\n---', 4);
|
|
27
|
+
|
|
28
|
+
if (!content.startsWith('---\n') || fmEnd === -1) {
|
|
29
|
+
return appendMarkdown(content, urlPath);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const frontmatter = content.slice(4, fmEnd);
|
|
33
|
+
|
|
34
|
+
if (!/^images:/m.test(frontmatter)) {
|
|
35
|
+
return appendMarkdown(content, urlPath);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return content.replace(
|
|
39
|
+
/^(images:)((?:\n {2}-[^\n]*)*)/m,
|
|
40
|
+
(_, key, block) => {
|
|
41
|
+
const existing = block.split('\n').filter((line) => /^ {2}- \S/.test(line));
|
|
42
|
+
|
|
43
|
+
return [key, ...existing, ` - ${urlPath}`].join('\n');
|
|
44
|
+
},
|
|
45
|
+
);
|
|
40
46
|
}
|
|
41
47
|
|
|
42
48
|
function copyToClipboard(text) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
49
|
+
const platform = process.platform;
|
|
50
|
+
|
|
51
|
+
if (platform === 'darwin') {
|
|
52
|
+
execSync(`echo ${JSON.stringify(text)} | pbcopy`);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (platform === 'win32') {
|
|
57
|
+
execFileSync('clip', { input: text });
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const tools = [
|
|
62
|
+
['xclip', ['-selection', 'clipboard']],
|
|
63
|
+
['xsel', ['--clipboard', '--input']],
|
|
64
|
+
['wl-copy', []],
|
|
65
|
+
];
|
|
66
|
+
|
|
67
|
+
for (const [cmd, args] of tools) {
|
|
68
|
+
try {
|
|
69
|
+
execFileSync(cmd, args, { input: text });
|
|
70
|
+
return;
|
|
71
|
+
} catch {
|
|
72
|
+
// try next
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
console.log('(clipboard unavailable — copy the path above manually)');
|
|
66
76
|
}
|
|
67
77
|
|
|
68
78
|
if (require.main === module) {
|
|
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
|
-
|
|
79
|
+
config.requireHugoSite();
|
|
80
|
+
|
|
81
|
+
const args = process.argv.slice(2);
|
|
82
|
+
|
|
83
|
+
if (args.length === 0) {
|
|
84
|
+
console.error('Usage: pick-image <image-file> [content-file]');
|
|
85
|
+
process.exit(1);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const imageFile = path.resolve(args[0]);
|
|
89
|
+
const contentFile = args[1] ? path.resolve(args[1]) : null;
|
|
90
|
+
|
|
91
|
+
if (!fs.existsSync(imageFile)) {
|
|
92
|
+
console.error(`Image not found: ${imageFile}`);
|
|
93
|
+
process.exit(1);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const ext = path.extname(imageFile).toLowerCase();
|
|
97
|
+
|
|
98
|
+
if (!IMAGE_EXTS.has(ext)) {
|
|
99
|
+
console.error(`Unsupported image format: ${ext}`);
|
|
100
|
+
process.exit(1);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const now = new Date();
|
|
104
|
+
const year = now.getFullYear().toString();
|
|
105
|
+
const month = String(now.getMonth() + 1).padStart(2, '0');
|
|
106
|
+
const filename = path.basename(imageFile);
|
|
107
|
+
const destDir = path.join(process.cwd(), config.imagesDir, year, month);
|
|
108
|
+
const destFile = path.join(destDir, filename);
|
|
109
|
+
const urlPath = urlFromImagesDir(config.imagesDir, year, month, filename);
|
|
110
|
+
|
|
111
|
+
fs.mkdirSync(destDir, { recursive: true });
|
|
112
|
+
|
|
113
|
+
if (path.resolve(imageFile) !== path.resolve(destFile)) {
|
|
114
|
+
if (fs.existsSync(destFile)) {
|
|
115
|
+
console.error(`File already exists at destination: ${destFile}`);
|
|
116
|
+
process.exit(1);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
fs.renameSync(imageFile, destFile);
|
|
120
|
+
console.log(`Moved to: ${destFile}`);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (!contentFile) {
|
|
124
|
+
copyToClipboard(urlPath);
|
|
125
|
+
console.log(`Path copied to clipboard: ${urlPath}`);
|
|
126
|
+
process.exit(0);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (!fs.existsSync(contentFile)) {
|
|
130
|
+
console.error(`Content file not found: ${contentFile}`);
|
|
131
|
+
process.exit(1);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const original = fs.readFileSync(contentFile, 'utf8');
|
|
135
|
+
const updated = insertImage(original, urlPath);
|
|
136
|
+
|
|
137
|
+
fs.writeFileSync(contentFile, updated);
|
|
138
|
+
console.log(`Updated: ${path.relative(process.cwd(), contentFile)}`);
|
|
125
139
|
}
|
|
126
140
|
|
|
127
141
|
module.exports = { urlFromImagesDir, appendMarkdown, insertImage };
|
package/src/pick-image.test.mjs
CHANGED
|
@@ -10,15 +10,15 @@ const { urlFromImagesDir, appendMarkdown, insertImage } = require('./pick-image.
|
|
|
10
10
|
// ---------------------------------------------------------------------------
|
|
11
11
|
|
|
12
12
|
test('urlFromImagesDir: strips assets/ prefix', () => {
|
|
13
|
-
|
|
13
|
+
assert.equal(urlFromImagesDir('assets/images', '2026', '05', 'photo.jpg'), '/images/2026/05/photo.jpg');
|
|
14
14
|
});
|
|
15
15
|
|
|
16
16
|
test('urlFromImagesDir: preserves dir without assets/ prefix', () => {
|
|
17
|
-
|
|
17
|
+
assert.equal(urlFromImagesDir('images', '2026', '05', 'photo.jpg'), '/images/2026/05/photo.jpg');
|
|
18
18
|
});
|
|
19
19
|
|
|
20
20
|
test('urlFromImagesDir: preserves custom directory name', () => {
|
|
21
|
-
|
|
21
|
+
assert.equal(urlFromImagesDir('assets/media', '2026', '05', 'photo.jpg'), '/media/2026/05/photo.jpg');
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
// ---------------------------------------------------------------------------
|
|
@@ -26,18 +26,21 @@ test('urlFromImagesDir: preserves custom directory name', () => {
|
|
|
26
26
|
// ---------------------------------------------------------------------------
|
|
27
27
|
|
|
28
28
|
test('appendMarkdown: appends image tag with blank line separator', () => {
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
const result = appendMarkdown('Body text.\n', '/images/2026/05/photo.jpg');
|
|
30
|
+
|
|
31
|
+
assert.equal(result, 'Body text.\n\n\n');
|
|
31
32
|
});
|
|
32
33
|
|
|
33
34
|
test('appendMarkdown: trims trailing whitespace before appending', () => {
|
|
34
|
-
|
|
35
|
-
|
|
35
|
+
const result = appendMarkdown('Body text.\n\n\n', '/images/2026/05/photo.jpg');
|
|
36
|
+
|
|
37
|
+
assert.equal(result, 'Body text.\n\n\n');
|
|
36
38
|
});
|
|
37
39
|
|
|
38
40
|
test('appendMarkdown: works when content has no trailing newline', () => {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
+
const result = appendMarkdown('Body text.', '/images/2026/05/photo.jpg');
|
|
42
|
+
|
|
43
|
+
assert.equal(result, 'Body text.\n\n\n');
|
|
41
44
|
});
|
|
42
45
|
|
|
43
46
|
// ---------------------------------------------------------------------------
|
|
@@ -45,17 +48,19 @@ test('appendMarkdown: works when content has no trailing newline', () => {
|
|
|
45
48
|
// ---------------------------------------------------------------------------
|
|
46
49
|
|
|
47
50
|
test('insertImage: appends markdown when no frontmatter', () => {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
51
|
+
const content = 'Just a plain body.\n';
|
|
52
|
+
const result = insertImage(content, '/images/2026/05/photo.jpg');
|
|
53
|
+
|
|
54
|
+
assert.ok(result.includes(''));
|
|
55
|
+
assert.ok(result.includes('Just a plain body.'));
|
|
52
56
|
});
|
|
53
57
|
|
|
54
58
|
test('insertImage: appends markdown when frontmatter has no images: field', () => {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
+
const content = '---\ntitle: "My Post"\n---\nBody text.\n';
|
|
60
|
+
const result = insertImage(content, '/images/2026/05/photo.jpg');
|
|
61
|
+
|
|
62
|
+
assert.ok(result.includes(''));
|
|
63
|
+
assert.ok(result.includes('Body text.'));
|
|
59
64
|
});
|
|
60
65
|
|
|
61
66
|
// ---------------------------------------------------------------------------
|
|
@@ -63,48 +68,54 @@ test('insertImage: appends markdown when frontmatter has no images: field', () =
|
|
|
63
68
|
// ---------------------------------------------------------------------------
|
|
64
69
|
|
|
65
70
|
test('insertImage: replaces placeholder ( -) with real path', () => {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
+
const content = '---\ntitle: "My Post"\nimages:\n -\n---\nBody.\n';
|
|
72
|
+
const result = insertImage(content, '/images/2026/05/photo.jpg');
|
|
73
|
+
|
|
74
|
+
assert.ok(result.includes(' - /images/2026/05/photo.jpg'));
|
|
75
|
+
assert.ok(!result.includes('\n -\n'));
|
|
76
|
+
assert.ok(!result.includes(');
|
|
71
77
|
});
|
|
72
78
|
|
|
73
79
|
test('insertImage: appends to existing single-item images array', () => {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
80
|
+
const content = '---\ntitle: "My Post"\nimages:\n - /images/2026/01/old.jpg\n---\nBody.\n';
|
|
81
|
+
const result = insertImage(content, '/images/2026/05/new.jpg');
|
|
82
|
+
|
|
83
|
+
assert.ok(result.includes(' - /images/2026/01/old.jpg'));
|
|
84
|
+
assert.ok(result.includes(' - /images/2026/05/new.jpg'));
|
|
85
|
+
assert.ok(!result.includes(');
|
|
79
86
|
});
|
|
80
87
|
|
|
81
88
|
test('insertImage: appends to existing multi-item images array', () => {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
89
|
+
const content = '---\nimages:\n - /images/2026/01/a.jpg\n - /images/2026/02/b.jpg\n---\n';
|
|
90
|
+
const result = insertImage(content, '/images/2026/05/c.jpg');
|
|
91
|
+
const lines = result.split('\n');
|
|
92
|
+
const imgLines = lines.filter((l) => l.startsWith(' - '));
|
|
93
|
+
|
|
94
|
+
assert.equal(imgLines.length, 3);
|
|
95
|
+
assert.equal(imgLines[2], ' - /images/2026/05/c.jpg');
|
|
88
96
|
});
|
|
89
97
|
|
|
90
98
|
test('insertImage: does not duplicate existing items', () => {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
99
|
+
const content = '---\nimages:\n - /images/2026/01/a.jpg\n---\n';
|
|
100
|
+
const result = insertImage(content, '/images/2026/05/b.jpg');
|
|
101
|
+
const count = (result.match(/ {2}- \/images/g) || []).length;
|
|
102
|
+
|
|
103
|
+
assert.equal(count, 2);
|
|
95
104
|
});
|
|
96
105
|
|
|
97
106
|
test('insertImage: leaves other frontmatter fields untouched', () => {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
107
|
+
const content = '---\ntitle: "My Post"\nimages:\n -\ncategories:\n - Tech\ntags:\n - go\n---\nBody.\n';
|
|
108
|
+
const result = insertImage(content, '/images/2026/05/photo.jpg');
|
|
109
|
+
|
|
110
|
+
assert.ok(result.includes('title: "My Post"'));
|
|
111
|
+
assert.ok(result.includes('categories:\n - Tech'));
|
|
112
|
+
assert.ok(result.includes('tags:\n - go'));
|
|
113
|
+
assert.ok(result.includes('Body.'));
|
|
104
114
|
});
|
|
105
115
|
|
|
106
116
|
test('insertImage: body content is preserved when updating frontmatter', () => {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
117
|
+
const content = '---\ntitle: "My Post"\nimages:\n -\n---\nThis is the body.\n';
|
|
118
|
+
const result = insertImage(content, '/images/2026/05/photo.jpg');
|
|
119
|
+
|
|
120
|
+
assert.ok(result.includes('This is the body.'));
|
|
110
121
|
});
|