Skip to content

Commit 4f13659

Browse files
committed
Breadcrumb support added to CLI Method - closes #17
1 parent c72e7d9 commit 4f13659

File tree

9 files changed

+126
-107
lines changed

9 files changed

+126
-107
lines changed

.richiejs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@
213213
"restaurant": false,
214214
"recipe": true
215215
},
216-
"isProductVar": true
216+
"isProductVar": true,
217+
"breadcrumb":false
217218
}
218-
}
219+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div align="center">
22

3-
![Richi JS logo](./logo/rjs-icon.svg)
3+
![Richi JS logo](./logo/logo.png)
44

55
# Richie JS - Powerful SEO Tool for Generating Rich Results
66

bin/richieMaker.ts

Lines changed: 71 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -62,68 +62,69 @@ export async function makeRichie(
6262
new Promise((resolve, reject) => {
6363
readFile(file, { encoding: "utf8" })
6464
.then((htmlText) => {
65-
const neededRichies: richies[] =
66-
richieTypeAcquisition(htmlText);
67-
68-
neededRichies.forEach((richieName) => {
69-
const dest = join(
70-
process.cwd(),
71-
options.destDir ?? "",
72-
dirname(relative(process.cwd(), file)),
73-
basename(file),
74-
);
75-
76-
try {
77-
//make dir
78-
mkdirSync(dirname(dest));
79-
} catch (err: any) {
80-
/* console.log(err.code); */
81-
}
82-
83-
//carousal handler
84-
if (
85-
richieCarousals.includes(richieName) ||
86-
richieName === "product"
87-
) {
88-
switch (richieName) {
89-
case "recipe":
90-
if (isCarousals.recipe) {
91-
richieName = "crecipe";
92-
}
93-
break;
94-
case "movie":
95-
if (isCarousals.movie) {
96-
richieName = "cmovie";
97-
}
98-
break;
99-
case "restaurant":
100-
if (isCarousals.restaurant) {
101-
richieName = "crestaurant";
102-
}
103-
break;
104-
case "course":
105-
if (isCarousals.course) {
106-
richieName = "ccourse";
107-
}
108-
break;
109-
case "product":
110-
if (preference.isProductVar) {
111-
richieName = "productwv";
112-
}
113-
break;
114-
}
65+
let neededRichies: richies[] = richieTypeAcquisition(htmlText);
66+
67+
const dest = join(
68+
process.cwd(),
69+
options.destDir ?? "",
70+
dirname(relative(process.cwd(), file)),
71+
basename(file),
72+
);
73+
74+
try {
75+
//make dir
76+
mkdirSync(dirname(dest), { recursive: true });
77+
} catch (err: any) {
78+
console.log(err);
79+
process.exit(1);
80+
}
81+
82+
neededRichies = neededRichies.map((richieName: richies) => {
83+
switch (richieName) {
84+
case "recipe":
85+
if (isCarousals.recipe) {
86+
return "crecipe";
87+
}
88+
return richieName;
89+
90+
case "movie":
91+
if (isCarousals.movie) {
92+
return "cmovie";
93+
}
94+
return richieName;
95+
96+
case "restaurant":
97+
if (isCarousals.restaurant) {
98+
return "crestaurant";
99+
}
100+
return richieName;
101+
102+
case "course":
103+
if (isCarousals.course) {
104+
return "ccourse";
105+
}
106+
return richieName;
107+
108+
case "product":
109+
if (preference.isProductVar) {
110+
return "productwv";
111+
}
112+
return richieName;
113+
114+
default:
115+
return richieName;
115116
}
116117

117118
//
118-
119-
richie(richieName, file, dest)
120-
.then(() => {
121-
resolve();
122-
})
123-
.catch((err) => {
124-
reject(err);
125-
});
126119
});
120+
121+
richie(neededRichies, file, dest)
122+
.then(() => {
123+
resolve();
124+
})
125+
.catch((err) => {
126+
reject(err);
127+
});
127128
})
128129
.catch((err) => {
129130
reject(err);
@@ -135,21 +136,22 @@ export async function makeRichie(
135136
await Promise.all(concurrentOPS);
136137
}
137138

138-
// File type acquisition based on artifacts
139+
// File type acquisition based on content
139140
function richieTypeAcquisition(htmlText: string): richies[] {
140141
const availableTypes: richies[] = [];
141142

142143
//a=len(2)
143-
const noIDTypes: richies[] = ["breadcrumb", "searchbox"];
144+
/* const noIDTypes: richies[] = ["breadcrumb", "searchbox"]; */
144145

145146
//b=len(5) a+b = 7
146-
const IDTypesVars: richies[] = [
147+
/* Identifiable by content but controlled by configuration file */
148+
/* const IDTypesVars: richies[] = [
147149
"crecipe",
148150
"cmovie",
149-
"course",
151+
"ccourse",
150152
"crestaurant",
151153
"productwv",
152-
];
154+
]; */
153155

154156
//c=len(13) a+b+c = 20
155157
const IDTypesRecord: Partial<Record<richies, string>> = {
@@ -183,5 +185,11 @@ function richieTypeAcquisition(htmlText: string): richies[] {
183185
}
184186
});
185187

188+
/* breadcrumb controlled by configfile */
189+
if (preference.breadcrumb) {
190+
availableTypes.push("breadcrumb");
191+
}
192+
/* */
193+
186194
return availableTypes;
187195
}

intellisense/richiejs-config-schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,11 @@
931931
"type": "boolean",
932932
"default": false,
933933
"markdownDescription": "[Guide Link](https://cresteem.com/opensource/richie-js)"
934+
},
935+
"breadcrumb":{
936+
"type": "boolean",
937+
"default": false,
938+
"markdownDescription": "[Guide Link](https://cresteem.com/opensource/richie-js)"
934939
}
935940
}
936941
}

lib/aggregator.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,12 @@ export function breadCrumb(htmlPath: string): breadCrumbListOptions {
216216
const levelCounts: number =
217217
sourceIsIndex ? pathTree.length - 1 : pathTree.length;
218218

219-
/* In first iteration no need to check file existance */
220-
let firstIteration: boolean = true;
221-
222219
let realLevel: number = levelCounts; //to track real chronological level according to web protocol
223220

224221
for (let i: number = 0; i < levelCounts; i++) {
225222
/* assume in first iteration file
226223
always exist so skip existance check */
227-
if (firstIteration) {
224+
if (i === 0) {
228225
let itemUrl: string = pathTree.join(sep);
229226

230227
const preserveBasename: boolean = sourceIsIndex ? false : true;
@@ -242,9 +239,6 @@ export function breadCrumb(htmlPath: string): breadCrumbListOptions {
242239
/* if source is index pop two times otherwise pop one time*/
243240
//EX: L1/L2/L3/index.html => L1/L2
244241
if (sourceIsIndex) pathTree.pop();
245-
246-
//switching flag for next iterations
247-
firstIteration = false;
248242
} else {
249243
//check if index html is available for each levels
250244
// L1/L2 => L1/L2/index.html

lib/options.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,9 @@ export interface configurationOptions {
801801
recipe: boolean;
802802
};
803803
isProductVar: boolean;
804+
805+
//15/11/2024
806+
breadcrumb: boolean;
804807
};
805808
}
806809

lib/utilities.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,12 @@ export function combineAggregateRatings(
292292
export function createJsonLD(
293293
innerDataObject: Record<string, any>,
294294
): string {
295-
const jsonLD = `<script type="application/ld+json">${JSON.stringify(
296-
innerDataObject,
297-
)}</script>`;
295+
const jsonLD =
296+
innerDataObject ?
297+
`<script type="application/ld+json">${JSON.stringify(
298+
innerDataObject,
299+
)}</script>`
300+
: "";
298301
return jsonLD;
299302
}
300303

richie.ts

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ import {
3030
richieOPS,
3131
richies,
3232
} from "./lib/options";
33-
import { createJsonLD, writeOutput } from "./lib/utilities";
3433
import { sweep } from "./lib/sweeper";
34+
import { createJsonLD, writeOutput } from "./lib/utilities";
3535

3636
const functionMap: Record<richies, richieOPS> = {
3737
article: {
@@ -117,7 +117,7 @@ const functionMap: Record<richies, richieOPS> = {
117117
};
118118

119119
export async function richie(
120-
richieName: richies,
120+
richieNames: richies[],
121121
filepath: string,
122122
destinationPath: string = "",
123123
): Promise<void> {
@@ -126,40 +126,45 @@ export async function richie(
126126

127127
const source: string = await readFile(filepath, { encoding: "utf8" });
128128

129-
//standardize parameters
130-
const aggregatorParams: string[] | boolean =
131-
richieGroupA.includes(richieName) ? [source]
132-
: richieGroupB.includes(richieName) ? [source, filepath]
133-
: richieGroupC.includes(richieName) ? [filepath]
134-
: false;
129+
let richResultSnippets: string = "";
130+
let cleanSource: string | null = null;
135131

136132
return new Promise(async (resolve, reject) => {
137-
if (!aggregatorParams) {
138-
reject(new Error("Unsupported Richie name"));
139-
} else {
140-
const aggregator: Function = functionMap[richieName].aggregator;
141-
const serializer: Function = functionMap[richieName].serializer;
133+
for (const richieName of richieNames) {
134+
//standardize parameters
135+
const aggregatorParams: string[] | boolean =
136+
richieGroupA.includes(richieName) ? [source]
137+
: richieGroupB.includes(richieName) ? [source, filepath]
138+
: richieGroupC.includes(richieName) ? [filepath]
139+
: false;
142140

143-
const aggregatedData = await aggregator(...aggregatorParams);
141+
if (!aggregatorParams) {
142+
reject(new Error("Unsupported Richie name"));
143+
} else {
144+
const aggregator: Function = functionMap[richieName].aggregator;
145+
const serializer: Function = functionMap[richieName].serializer;
144146

145-
const serializerParams: any[] =
146-
richieName === "productwv" ?
147-
[...Object.values(aggregatedData)] // [productMeta,variesBy]
148-
: richieName === "product" ?
149-
[Object.values(aggregatedData)[0]] // [productMeta]
150-
: [aggregatedData];
147+
const aggregatedData = await aggregator(...aggregatorParams);
151148

152-
const serializedData = serializer(...serializerParams);
153-
const richResultSnippet = createJsonLD(serializedData);
154-
const cleanSource = sweep(richieName, source);
149+
const serializerParams: any[] =
150+
richieName === "productwv" ?
151+
[...Object.values(aggregatedData)] // [productMeta,variesBy]
152+
: richieName === "product" ?
153+
[Object.values(aggregatedData)[0]] // [productMeta]
154+
: [aggregatedData];
155155

156-
writeOutput(cleanSource, destinationFile, richResultSnippet)
157-
.then(() => {
158-
resolve();
159-
})
160-
.catch((error) => {
161-
reject(error);
162-
});
156+
const serializedData = serializer(...serializerParams);
157+
richResultSnippets += createJsonLD(serializedData);
158+
cleanSource = sweep(richieName, cleanSource ?? source);
159+
}
163160
}
161+
162+
writeOutput(cleanSource as string, destinationFile, richResultSnippets)
163+
.then(() => {
164+
resolve();
165+
})
166+
.catch((error) => {
167+
reject(error);
168+
});
164169
});
165170
}

test/main.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function runAll(): Promise<string> {
8686
//nothing to do
8787
} finally {
8888
//make op dir
89-
mkdirSync(opfolder);
89+
mkdirSync(opfolder, { recursive: true });
9090
}
9191

9292
const testOps: Promise<void>[] = [];

0 commit comments

Comments
 (0)