Skip to content

Commit 1baaf93

Browse files
authored
Merge pull request #55 from jverneaut/48-allow-overriding-generated-files
48 allow overriding generated files
2 parents 168edb3 + 2718197 commit 1baaf93

File tree

5 files changed

+139
-55
lines changed

5 files changed

+139
-55
lines changed

__tests__/parser.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ const UNPROCESSABLE_FIXTURES_DIR = path.join(
1414
"fixtures/unprocessable",
1515
);
1616

17-
const getGenerateFileByType = (generateFiles, type) =>
18-
generateFiles[0].files.filter((file) => file.type === type)[0].content;
17+
const getGenerateFileByFilename = (generateFiles, filename) =>
18+
generateFiles.filter((file) => file.filename === filename)[0].content;
1919

2020
const generateFiles = async (caseDir) => {
2121
const htmlToGutenberg = new HTMLToGutenberg({
@@ -25,10 +25,10 @@ const generateFiles = async (caseDir) => {
2525
const generatedFiles = await htmlToGutenberg.generateFiles();
2626

2727
return {
28-
index: getGenerateFileByType(generatedFiles, "index"),
29-
edit: getGenerateFileByType(generatedFiles, "edit"),
30-
php: getGenerateFileByType(generatedFiles, "php"),
31-
json: getGenerateFileByType(generatedFiles, "json"),
28+
index: getGenerateFileByFilename(generatedFiles, "index.js"),
29+
edit: getGenerateFileByFilename(generatedFiles, "edit.js"),
30+
php: getGenerateFileByFilename(generatedFiles, "render.php"),
31+
json: getGenerateFileByFilename(generatedFiles, "block.json"),
3232
};
3333
};
3434

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
sidebar_position: 1
3+
---
4+
5+
# Overriding the files generated by HTML To Gutenberg
6+
7+
By default, HTML To Gutenberg automatically generates the following files for each block found in your input directory:
8+
9+
- `index.js`
10+
- `edit.js`
11+
- `block.json`
12+
- `render.php`
13+
14+
However, you can override any of these files by simply placing specially named files alongside the corresponding `.html` source file.
15+
16+
**To override a file, create a new file with the following format:**
17+
18+
```txt
19+
[block-slug].[filename].[extension]
20+
```
21+
22+
For example, if you have a block defined in `blocks/hero.html`, you can override its generated `render.php` file by creating:
23+
24+
```txt
25+
blocks/hero.render.php
26+
```
27+
28+
Similarly, you can override:
29+
30+
```txt
31+
blocks/hero.index.js
32+
blocks/hero.edit.js
33+
blocks/hero.block.json
34+
```
35+
36+
These files will be copied **as-is** into the block's output folder and will not be overwritten by the generator.
37+
38+
In the future, you’ll also be able to use this override system to provide editor and frontend specific styles and scripts.
39+
These features are not implemented yet, but this flexible naming convention is designed to support them seamlessly.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@jverneaut/html-to-gutenberg",
3-
"version": "1.7.1",
3+
"version": "1.7.2",
44
"main": "index.js",
55
"repository": {
66
"type": "git",

src/HTMLToGutenberg.js

Lines changed: 87 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ class HTMLToGutenberg {
6868

6969
try {
7070
for (const HTMLFile of this.HTMLFiles) {
71-
const files = [];
72-
7371
const HTMLFileContent = fs.readFileSync(HTMLFile, "utf-8");
7472

7573
const blockData = await getBlockData(HTMLFileContent, {
@@ -89,14 +87,30 @@ class HTMLToGutenberg {
8987
printRenderPHP(blockData),
9088
]);
9189

92-
files.push({ type: "index", content: indexJS });
93-
files.push({ type: "edit", content: editJS });
94-
files.push({ type: "json", content: blockJSON });
95-
files.push({ type: "php", content: renderPHP });
90+
const blockPath = this.generateBlockPath(HTMLFile);
91+
92+
generatedFiles.push({
93+
filename: "index.js",
94+
content: indexJS,
95+
blockPath,
96+
});
97+
98+
generatedFiles.push({
99+
filename: "edit.js",
100+
content: editJS,
101+
blockPath,
102+
});
103+
104+
generatedFiles.push({
105+
filename: "block.json",
106+
content: blockJSON,
107+
blockPath,
108+
});
96109

97110
generatedFiles.push({
98-
source: HTMLFile,
99-
files,
111+
filename: "render.php",
112+
content: renderPHP,
113+
blockPath,
100114
});
101115
}
102116
} catch (err) {
@@ -106,55 +120,81 @@ class HTMLToGutenberg {
106120
return generatedFiles;
107121
}
108122

123+
async getFilesOverrides() {
124+
const overrides = [];
125+
126+
const htmlFiles = this.HTMLFiles.map((htmlPath) => ({
127+
fullPath: path.resolve(htmlPath),
128+
baseName: path.basename(htmlPath, ".html"),
129+
dir: path.dirname(htmlPath),
130+
}));
131+
132+
const htmlFilesMap = new Map();
133+
for (const { baseName, dir } of htmlFiles) {
134+
htmlFilesMap.set(path.join(dir, baseName), true);
135+
}
136+
137+
const allFiles = glob.sync(path.join(this.inputDirectory, "/**/*.*"));
138+
139+
for (const file of allFiles) {
140+
const resolved = path.resolve(file);
141+
142+
if (resolved.endsWith(".html")) continue;
143+
144+
const filename = path.basename(file);
145+
const dirname = path.dirname(file);
146+
147+
const match = filename.match(/^(.+?)\.(.+?)\.(.+)$/);
148+
if (!match) continue;
149+
150+
const [_, blockSlug, innerName, ext] = match;
151+
const htmlFileKey = path.join(dirname, blockSlug);
152+
153+
if (!htmlFilesMap.has(htmlFileKey)) continue;
154+
155+
const blockPath = path.join(this.outputDirectory, blockSlug);
156+
const content = fs.readFileSync(resolved, "utf-8");
157+
158+
overrides.push({
159+
filename: `${innerName}.${ext}`,
160+
content,
161+
blockPath,
162+
});
163+
}
164+
165+
return overrides;
166+
}
167+
109168
createDirectoryIfNotExists(directoryPath) {
110169
try {
111170
fs.mkdirSync(directoryPath, { recursive: true });
112171
} catch {}
113172
}
114173

115-
writeFiles(generatedFiles) {
116-
const generatedBlocksPaths = [];
174+
cleanDirectory(directoryPath) {
175+
const filesInDirectory = fs.readdirSync(directoryPath);
117176

118-
generatedFiles.forEach((generatedFile) => {
119-
const blockPath = this.generateBlockPath(generatedFile.source);
120-
this.createDirectoryIfNotExists(blockPath);
177+
for (const file of filesInDirectory) {
178+
if (!["block.json", "edit.js", "index.js", "render.php"].includes(file)) {
179+
fs.unlinkSync(path.join(directoryPath, file), (err) => {
180+
if (err) throw err;
181+
});
182+
}
183+
}
184+
}
121185

122-
generatedBlocksPaths.push(blockPath);
123-
124-
generatedFile.files.forEach(({ type, content }) => {
125-
switch (type) {
126-
case "index":
127-
fs.writeFileSync(
128-
path.join(blockPath, "index.js"),
129-
content,
130-
"utf-8",
131-
);
132-
break;
133-
134-
case "edit":
135-
fs.writeFileSync(path.join(blockPath, "edit.js"), content, "utf-8");
136-
break;
137-
138-
case "json":
139-
fs.writeFileSync(
140-
path.join(blockPath, "block.json"),
141-
content,
142-
"utf-8",
143-
);
144-
break;
145-
146-
case "php":
147-
fs.writeFileSync(
148-
path.join(blockPath, "render.php"),
149-
content,
150-
"utf-8",
151-
);
152-
break;
153-
}
154-
});
186+
cleanOutputAndWriteFiles(files) {
187+
const blockPaths = new Set(files.map((file) => file.blockPath));
188+
189+
blockPaths.forEach((blockPath) => {
190+
this.createDirectoryIfNotExists(blockPath);
191+
this.cleanDirectory(blockPath);
155192
});
156193

157-
return generatedBlocksPaths;
194+
files.forEach((file) => {
195+
const { blockPath, filename, content } = file;
196+
fs.writeFileSync(path.join(blockPath, filename), content, "utf-8");
197+
});
158198
}
159199

160200
removeDeletedBlocks() {

src/HTMLToGutenbergPlugin.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ class HTMLToGutenbergPlugin {
5353

5454
async generateAndWriteFiles() {
5555
const generatedFiles = await this.htmlToGutenberg.generateFiles();
56-
this.htmlToGutenberg.writeFiles(generatedFiles);
56+
const filesOverrides = await this.htmlToGutenberg.getFilesOverrides();
57+
58+
this.htmlToGutenberg.cleanOutputAndWriteFiles([
59+
...generatedFiles,
60+
...filesOverrides,
61+
]);
5762
}
5863
}
5964

0 commit comments

Comments
 (0)