@blankgrimoire/runtime 1.1.0 → 1.2.0
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/dist/engine.d.ts +3 -1
- package/dist/engine.js +63 -0
- package/dist/runtime.js +53 -0
- package/package.json +2 -2
package/dist/engine.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Chapter, type ContentRating, type MangaDetails, type MangaStatus, type MangaSummary, type Page, type PagedResult, type SearchQuery, type SiteContext, type ThemeAdapter, type ThemeSelectors, type ThemeSelectorsOverrides } from '@blankgrimoire/types';
|
|
1
|
+
import { type Chapter, type ContentRating, type ExtractedGenres, type GenreMapping, type MangaDetails, type MangaStatus, type MangaSummary, type Page, type PagedResult, type SearchQuery, type SiteContext, type ThemeAdapter, type ThemeSelectors, type ThemeSelectorsOverrides } from '@blankgrimoire/types';
|
|
2
2
|
import type { DocumentLike, ParseHTML, WindowLike } from './dom.js';
|
|
3
3
|
export interface ThemeRuntimeOptions {
|
|
4
4
|
adapter: ThemeAdapter;
|
|
@@ -37,6 +37,8 @@ export declare function extractDetails(runtime: ThemeRuntimeOptions, key: string
|
|
|
37
37
|
export declare function extractChapters(runtime: ThemeRuntimeOptions, key: string): Promise<Chapter[]>;
|
|
38
38
|
/** Extract chapter pages. Hooks take precedence over declarative strategies. */
|
|
39
39
|
export declare function extractPages(runtime: ThemeRuntimeOptions, keys: PageKeys): Promise<Page[]>;
|
|
40
|
+
/** Extract genre filters from a page using the theme's genre mapping. */
|
|
41
|
+
export declare function extractGenres(runtime: ThemeRuntimeOptions, mapping: GenreMapping): Promise<ExtractedGenres>;
|
|
40
42
|
/** Backwards-compatible name for page extraction. */
|
|
41
43
|
export declare const extract: typeof extractPages;
|
|
42
44
|
/**
|
package/dist/engine.js
CHANGED
|
@@ -298,6 +298,54 @@ export async function extractPages(runtime, keys) {
|
|
|
298
298
|
}
|
|
299
299
|
return [];
|
|
300
300
|
}
|
|
301
|
+
/** Extract genre filters from a page using the theme's genre mapping. */
|
|
302
|
+
export async function extractGenres(runtime, mapping) {
|
|
303
|
+
assertAdapterAPIVersion(runtime.adapter);
|
|
304
|
+
const document = runtime.document;
|
|
305
|
+
const filters = [];
|
|
306
|
+
if (!mapping.source) {
|
|
307
|
+
return { filters };
|
|
308
|
+
}
|
|
309
|
+
const elements = document.querySelectorAll(mapping.source);
|
|
310
|
+
for (const element of Array.from(elements)) {
|
|
311
|
+
const displayName = mapping.displayNameAttr
|
|
312
|
+
? element.querySelector(mapping.displayNameAttr)?.textContent?.trim() || element.textContent?.trim()
|
|
313
|
+
: element.textContent?.trim();
|
|
314
|
+
if (!displayName)
|
|
315
|
+
continue;
|
|
316
|
+
let value = '';
|
|
317
|
+
if (mapping.valueAttr) {
|
|
318
|
+
value = element.getAttribute(mapping.valueAttr) || '';
|
|
319
|
+
}
|
|
320
|
+
else {
|
|
321
|
+
value = element.textContent?.trim() || '';
|
|
322
|
+
}
|
|
323
|
+
if (mapping.valuePattern) {
|
|
324
|
+
const regex = new RegExp(mapping.valuePattern);
|
|
325
|
+
const match = value.match(regex);
|
|
326
|
+
if (match && match[1]) {
|
|
327
|
+
value = match[1];
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
if (value) {
|
|
331
|
+
const href = element.getAttribute('href');
|
|
332
|
+
if (href) {
|
|
333
|
+
filters.push({
|
|
334
|
+
displayName: displayName.trim(),
|
|
335
|
+
value: value.trim(),
|
|
336
|
+
url: href
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
else {
|
|
340
|
+
filters.push({
|
|
341
|
+
displayName: displayName.trim(),
|
|
342
|
+
value: value.trim()
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
return { filters };
|
|
348
|
+
}
|
|
301
349
|
/** Backwards-compatible name for page extraction. */
|
|
302
350
|
export const extract = extractPages;
|
|
303
351
|
function asRecord(value) {
|
|
@@ -345,6 +393,21 @@ export async function dispatch(runtime, method, argsJSON) {
|
|
|
345
393
|
mangaKey: String(args.mangaKey ?? ''),
|
|
346
394
|
chapterKey: String(args.chapterKey ?? ''),
|
|
347
395
|
}));
|
|
396
|
+
case 'extractGenres': {
|
|
397
|
+
const argsRecord = args;
|
|
398
|
+
const mapping = {
|
|
399
|
+
source: String(argsRecord.source ?? ''),
|
|
400
|
+
};
|
|
401
|
+
if (argsRecord.displayNameAttr)
|
|
402
|
+
mapping.displayNameAttr = String(argsRecord.displayNameAttr);
|
|
403
|
+
if (argsRecord.valueAttr)
|
|
404
|
+
mapping.valueAttr = String(argsRecord.valueAttr);
|
|
405
|
+
if (argsRecord.valuePattern)
|
|
406
|
+
mapping.valuePattern = String(argsRecord.valuePattern);
|
|
407
|
+
if (argsRecord.filterParam)
|
|
408
|
+
mapping.filterParam = String(argsRecord.filterParam);
|
|
409
|
+
return toJSON(await extractGenres(runtime, mapping));
|
|
410
|
+
}
|
|
348
411
|
default:
|
|
349
412
|
throw new Error(`Unknown adapter method: ${method}`);
|
|
350
413
|
}
|
package/dist/runtime.js
CHANGED
|
@@ -767,6 +767,48 @@ var BlankGrimoireRuntime = (() => {
|
|
|
767
767
|
}
|
|
768
768
|
return [];
|
|
769
769
|
}
|
|
770
|
+
async function extractGenres(runtime, mapping) {
|
|
771
|
+
assertAdapterAPIVersion(runtime.adapter);
|
|
772
|
+
const document = runtime.document;
|
|
773
|
+
const filters = [];
|
|
774
|
+
if (!mapping.source) {
|
|
775
|
+
return { filters };
|
|
776
|
+
}
|
|
777
|
+
const elements = document.querySelectorAll(mapping.source);
|
|
778
|
+
for (const element of Array.from(elements)) {
|
|
779
|
+
const displayName = mapping.displayNameAttr ? element.querySelector(mapping.displayNameAttr)?.textContent?.trim() || element.textContent?.trim() : element.textContent?.trim();
|
|
780
|
+
if (!displayName) continue;
|
|
781
|
+
let value = "";
|
|
782
|
+
if (mapping.valueAttr) {
|
|
783
|
+
value = element.getAttribute(mapping.valueAttr) || "";
|
|
784
|
+
} else {
|
|
785
|
+
value = element.textContent?.trim() || "";
|
|
786
|
+
}
|
|
787
|
+
if (mapping.valuePattern) {
|
|
788
|
+
const regex = new RegExp(mapping.valuePattern);
|
|
789
|
+
const match = value.match(regex);
|
|
790
|
+
if (match && match[1]) {
|
|
791
|
+
value = match[1];
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
if (value) {
|
|
795
|
+
const href = element.getAttribute("href");
|
|
796
|
+
if (href) {
|
|
797
|
+
filters.push({
|
|
798
|
+
displayName: displayName.trim(),
|
|
799
|
+
value: value.trim(),
|
|
800
|
+
url: href
|
|
801
|
+
});
|
|
802
|
+
} else {
|
|
803
|
+
filters.push({
|
|
804
|
+
displayName: displayName.trim(),
|
|
805
|
+
value: value.trim()
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
return { filters };
|
|
811
|
+
}
|
|
770
812
|
var extract = extractPages;
|
|
771
813
|
function asRecord(value) {
|
|
772
814
|
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
@@ -809,6 +851,17 @@ var BlankGrimoireRuntime = (() => {
|
|
|
809
851
|
chapterKey: String(args.chapterKey ?? "")
|
|
810
852
|
})
|
|
811
853
|
);
|
|
854
|
+
case "extractGenres": {
|
|
855
|
+
const argsRecord = args;
|
|
856
|
+
const mapping = {
|
|
857
|
+
source: String(argsRecord.source ?? "")
|
|
858
|
+
};
|
|
859
|
+
if (argsRecord.displayNameAttr) mapping.displayNameAttr = String(argsRecord.displayNameAttr);
|
|
860
|
+
if (argsRecord.valueAttr) mapping.valueAttr = String(argsRecord.valueAttr);
|
|
861
|
+
if (argsRecord.valuePattern) mapping.valuePattern = String(argsRecord.valuePattern);
|
|
862
|
+
if (argsRecord.filterParam) mapping.filterParam = String(argsRecord.filterParam);
|
|
863
|
+
return toJSON(await extractGenres(runtime, mapping));
|
|
864
|
+
}
|
|
812
865
|
default:
|
|
813
866
|
throw new Error(`Unknown adapter method: ${method}`);
|
|
814
867
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blankgrimoire/runtime",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Declarative extraction runtime for BlankGrimoire theme adapters.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"directory": "packages/runtime"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@blankgrimoire/types": "1.
|
|
29
|
+
"@blankgrimoire/types": "1.2.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"esbuild": "^0.24.0",
|