Skip to content

Commit fef50e7

Browse files
authored
feat: Add custom chapter transformation for Lily on the Valley source (#1719) (#1910)
1 parent 2443d8f commit fef50e7

7 files changed

Lines changed: 70 additions & 4 deletions

File tree

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
.js
3+
plugins/multisrc/fictioneer/custom
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const scriptContent = loadedCheerio('script')
2+
.toArray()
3+
.map(script => loadedCheerio(script).html())
4+
.find(content => content && content.includes('var gib ='));
5+
6+
if (scriptContent) {
7+
const gibMatch = scriptContent.match(/var gib = (\[.*?\])/);
8+
if (gibMatch) {
9+
const gibArray = eval(gibMatch[1]) as string[];
10+
gibArray.forEach(cssClass => {
11+
loadedCheerio(`.${cssClass}`).remove();
12+
});
13+
}
14+
}
15+
16+
loadedCheerio('ruby').remove();
17+
18+
loadedCheerio('section#chapter-content p *').each((_, el) => {
19+
if (loadedCheerio(el).attr('data-fcnc-rev') !== '1') return;
20+
const textContent = loadedCheerio(el).text().trim();
21+
if (textContent) {
22+
loadedCheerio(el).replaceWith(
23+
Array.from(textContent).reverse().join(''),
24+
);
25+
}
26+
});
27+
28+
return (
29+
loadedCheerio('section#chapter-content > div')
30+
.html()
31+
?.normalize()
32+
.replace(/\u00A0/g, ' ')
33+
.replace(/\u2060/g, '')
34+
.replace(/­/g, '') // ­
35+
.replace(/[\u202F\u2007\u200B]/g, '') || ''
36+
);

plugins/multisrc/fictioneer/generator.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { readFileSync } from 'fs';
1+
import { existsSync, readFileSync } from 'fs';
22
import { fileURLToPath } from 'url';
33
import { dirname, join } from 'path';
44

@@ -20,8 +20,16 @@ const generator = function generator(source) {
2020
encoding: 'utf-8',
2121
});
2222

23+
const chapterTransformJsOrPath = source.options?.customJs?.chapterTransform;
24+
const chapterTransformPath = chapterTransformJsOrPath
25+
? join(folder, chapterTransformJsOrPath)
26+
: '';
27+
const chapterTransformJs = existsSync(chapterTransformPath)
28+
? readFileSync(chapterTransformPath, { encoding: 'utf-8' })
29+
: chapterTransformJsOrPath;
30+
2331
const pluginScript = `
24-
${readNovelFullTemplate}
32+
${readNovelFullTemplate.replace('// chapterTransformJs HERE', chapterTransformJs || '')}
2533
const plugin = new FictioneerPlugin(${JSON.stringify(source)});
2634
export default plugin;
2735
`.trim();

plugins/multisrc/fictioneer/sources.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
"sourceSite": "https://lilyonthevalley.com",
2222
"sourceName": "Lily on the Valley",
2323
"options": {
24+
"customJs": {
25+
"chapterTransform": "custom/lillyonthevalley/chapterTransform.js"
26+
},
27+
"versionIncrements": 1,
2428
"browsePage": "stories"
2529
}
2630
},

plugins/multisrc/fictioneer/template.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ class FictioneerPlugin implements Plugin.PluginBase {
135135
const body = await req.text();
136136

137137
const loadedCheerio = loadCheerio(body);
138+
139+
// chapterTransformJs HERE
140+
138141
return loadedCheerio('section#chapter-content > div').html() || '';
139142
}
140143

tsconfig.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,18 @@
118118

119119
"skipLibCheck": true /* Skip type checking all .d.ts files. */
120120
},
121-
"exclude": ["./plugins/**/*.broken.ts", "./.js/**/*", "scripts", "docs"]
121+
"exclude": [
122+
"./plugins/**/*.broken.ts",
123+
"./.js/**/*",
124+
"scripts/**",
125+
"docs/**",
126+
"./src/*.*",
127+
"./*.*",
128+
"public/**",
129+
".github/**",
130+
".husky/**",
131+
"node_modules/**",
132+
"./src/**",
133+
"./plugins/multisrc/*"
134+
]
122135
}

tsconfig.production.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
".github/**",
123123
".husky/**",
124124
"node_modules/**",
125-
"./src/**"
125+
"./src/**",
126+
"./plugins/multisrc/*"
126127
]
127128
}

0 commit comments

Comments
 (0)