Skip to content

Commit

Permalink
Breadcrumb support added to CLI Method - closes #17
Browse files Browse the repository at this point in the history
  • Loading branch information
darsan-in committed Nov 15, 2024
1 parent c72e7d9 commit 4f13659
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 107 deletions.
5 changes: 3 additions & 2 deletions .richiejs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@
"restaurant": false,
"recipe": true
},
"isProductVar": true
"isProductVar": true,
"breadcrumb":false
}
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div align="center">

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

# Richie JS - Powerful SEO Tool for Generating Rich Results

Expand Down
134 changes: 71 additions & 63 deletions bin/richieMaker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,68 +62,69 @@ export async function makeRichie(
new Promise((resolve, reject) => {
readFile(file, { encoding: "utf8" })
.then((htmlText) => {
const neededRichies: richies[] =
richieTypeAcquisition(htmlText);

neededRichies.forEach((richieName) => {
const dest = join(
process.cwd(),
options.destDir ?? "",
dirname(relative(process.cwd(), file)),
basename(file),
);

try {
//make dir
mkdirSync(dirname(dest));
} catch (err: any) {
/* console.log(err.code); */
}

//carousal handler
if (
richieCarousals.includes(richieName) ||
richieName === "product"
) {
switch (richieName) {
case "recipe":
if (isCarousals.recipe) {
richieName = "crecipe";
}
break;
case "movie":
if (isCarousals.movie) {
richieName = "cmovie";
}
break;
case "restaurant":
if (isCarousals.restaurant) {
richieName = "crestaurant";
}
break;
case "course":
if (isCarousals.course) {
richieName = "ccourse";
}
break;
case "product":
if (preference.isProductVar) {
richieName = "productwv";
}
break;
}
let neededRichies: richies[] = richieTypeAcquisition(htmlText);

const dest = join(
process.cwd(),
options.destDir ?? "",
dirname(relative(process.cwd(), file)),
basename(file),
);

try {
//make dir
mkdirSync(dirname(dest), { recursive: true });
} catch (err: any) {
console.log(err);
process.exit(1);
}

neededRichies = neededRichies.map((richieName: richies) => {
switch (richieName) {
case "recipe":
if (isCarousals.recipe) {
return "crecipe";
}
return richieName;

case "movie":
if (isCarousals.movie) {
return "cmovie";
}
return richieName;

case "restaurant":
if (isCarousals.restaurant) {
return "crestaurant";
}
return richieName;

case "course":
if (isCarousals.course) {
return "ccourse";
}
return richieName;

case "product":
if (preference.isProductVar) {
return "productwv";
}
return richieName;

default:
return richieName;
}

//

richie(richieName, file, dest)
.then(() => {
resolve();
})
.catch((err) => {
reject(err);
});
});

richie(neededRichies, file, dest)
.then(() => {
resolve();
})
.catch((err) => {
reject(err);
});
})
.catch((err) => {
reject(err);
Expand All @@ -135,21 +136,22 @@ export async function makeRichie(
await Promise.all(concurrentOPS);
}

// File type acquisition based on artifacts
// File type acquisition based on content
function richieTypeAcquisition(htmlText: string): richies[] {
const availableTypes: richies[] = [];

//a=len(2)
const noIDTypes: richies[] = ["breadcrumb", "searchbox"];
/* const noIDTypes: richies[] = ["breadcrumb", "searchbox"]; */

//b=len(5) a+b = 7
const IDTypesVars: richies[] = [
/* Identifiable by content but controlled by configuration file */
/* const IDTypesVars: richies[] = [
"crecipe",
"cmovie",
"course",
"ccourse",
"crestaurant",
"productwv",
];
]; */

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

/* breadcrumb controlled by configfile */
if (preference.breadcrumb) {
availableTypes.push("breadcrumb");
}
/* */

return availableTypes;
}
5 changes: 5 additions & 0 deletions intellisense/richiejs-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,11 @@
"type": "boolean",
"default": false,
"markdownDescription": "[Guide Link](https://cresteem.com/opensource/richie-js)"
},
"breadcrumb":{
"type": "boolean",
"default": false,
"markdownDescription": "[Guide Link](https://cresteem.com/opensource/richie-js)"
}
}
}
Expand Down
8 changes: 1 addition & 7 deletions lib/aggregator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,12 @@ export function breadCrumb(htmlPath: string): breadCrumbListOptions {
const levelCounts: number =
sourceIsIndex ? pathTree.length - 1 : pathTree.length;

/* In first iteration no need to check file existance */
let firstIteration: boolean = true;

let realLevel: number = levelCounts; //to track real chronological level according to web protocol

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

const preserveBasename: boolean = sourceIsIndex ? false : true;
Expand All @@ -242,9 +239,6 @@ export function breadCrumb(htmlPath: string): breadCrumbListOptions {
/* if source is index pop two times otherwise pop one time*/
//EX: L1/L2/L3/index.html => L1/L2
if (sourceIsIndex) pathTree.pop();

//switching flag for next iterations
firstIteration = false;
} else {
//check if index html is available for each levels
// L1/L2 => L1/L2/index.html
Expand Down
3 changes: 3 additions & 0 deletions lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,9 @@ export interface configurationOptions {
recipe: boolean;
};
isProductVar: boolean;

//15/11/2024
breadcrumb: boolean;
};
}

Expand Down
9 changes: 6 additions & 3 deletions lib/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,12 @@ export function combineAggregateRatings(
export function createJsonLD(
innerDataObject: Record<string, any>,
): string {
const jsonLD = `<script type="application/ld+json">${JSON.stringify(
innerDataObject,
)}</script>`;
const jsonLD =
innerDataObject ?
`<script type="application/ld+json">${JSON.stringify(
innerDataObject,
)}</script>`
: "";
return jsonLD;
}

Expand Down
65 changes: 35 additions & 30 deletions richie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import {
richieOPS,
richies,
} from "./lib/options";
import { createJsonLD, writeOutput } from "./lib/utilities";
import { sweep } from "./lib/sweeper";
import { createJsonLD, writeOutput } from "./lib/utilities";

const functionMap: Record<richies, richieOPS> = {
article: {
Expand Down Expand Up @@ -117,7 +117,7 @@ const functionMap: Record<richies, richieOPS> = {
};

export async function richie(
richieName: richies,
richieNames: richies[],
filepath: string,
destinationPath: string = "",
): Promise<void> {
Expand All @@ -126,40 +126,45 @@ export async function richie(

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

//standardize parameters
const aggregatorParams: string[] | boolean =
richieGroupA.includes(richieName) ? [source]
: richieGroupB.includes(richieName) ? [source, filepath]
: richieGroupC.includes(richieName) ? [filepath]
: false;
let richResultSnippets: string = "";
let cleanSource: string | null = null;

return new Promise(async (resolve, reject) => {
if (!aggregatorParams) {
reject(new Error("Unsupported Richie name"));
} else {
const aggregator: Function = functionMap[richieName].aggregator;
const serializer: Function = functionMap[richieName].serializer;
for (const richieName of richieNames) {
//standardize parameters
const aggregatorParams: string[] | boolean =
richieGroupA.includes(richieName) ? [source]
: richieGroupB.includes(richieName) ? [source, filepath]
: richieGroupC.includes(richieName) ? [filepath]
: false;

const aggregatedData = await aggregator(...aggregatorParams);
if (!aggregatorParams) {
reject(new Error("Unsupported Richie name"));
} else {
const aggregator: Function = functionMap[richieName].aggregator;
const serializer: Function = functionMap[richieName].serializer;

const serializerParams: any[] =
richieName === "productwv" ?
[...Object.values(aggregatedData)] // [productMeta,variesBy]
: richieName === "product" ?
[Object.values(aggregatedData)[0]] // [productMeta]
: [aggregatedData];
const aggregatedData = await aggregator(...aggregatorParams);

const serializedData = serializer(...serializerParams);
const richResultSnippet = createJsonLD(serializedData);
const cleanSource = sweep(richieName, source);
const serializerParams: any[] =
richieName === "productwv" ?
[...Object.values(aggregatedData)] // [productMeta,variesBy]
: richieName === "product" ?
[Object.values(aggregatedData)[0]] // [productMeta]
: [aggregatedData];

writeOutput(cleanSource, destinationFile, richResultSnippet)
.then(() => {
resolve();
})
.catch((error) => {
reject(error);
});
const serializedData = serializer(...serializerParams);
richResultSnippets += createJsonLD(serializedData);
cleanSource = sweep(richieName, cleanSource ?? source);
}
}

writeOutput(cleanSource as string, destinationFile, richResultSnippets)
.then(() => {
resolve();
})
.catch((error) => {
reject(error);
});
});
}
2 changes: 1 addition & 1 deletion test/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function runAll(): Promise<string> {
//nothing to do
} finally {
//make op dir
mkdirSync(opfolder);
mkdirSync(opfolder, { recursive: true });
}

const testOps: Promise<void>[] = [];
Expand Down

0 comments on commit 4f13659

Please sign in to comment.