Skip to content

Commit

Permalink
tag fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscoMoretti committed Jan 5, 2025
1 parent 98e0f3b commit 5c82b95
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 103 deletions.
135 changes: 49 additions & 86 deletions mdcast.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,101 +28,64 @@ const config: MdCastConfigInput = {
// id
// }
// }
tags_dictionary: [
{
id: "56744721958ef13879b94cad",
name: "JavaScript",
slug: "javascript",
},
{
id: "56744723958ef13879b954e0",
name: "TypeScript",
slug: "typescript",
},
{ id: "56744721958ef13879b94988", name: "Ubuntu", slug: "ubuntu" },
{ id: "56744723958ef13879b954f5", name: "UI", slug: "ui" },
{
id: "57323a8bae9d49b5a5a5b39c",
name: "Visual Studio Code",
slug: "vscode",
},
{ id: "56744723958ef13879b953f7", name: "Windows", slug: "windows" },
{ id: "56744723958ef13879b95434", name: "React", slug: "reactjs" },
{ id: "5682e64e2c29f7e0c86d024b", name: "services", slug: "services" },
{
id: "5f4ebbb150b5c61ec6ef4ad2",
name: "Tailwind CSS",
slug: "tailwind-css",
},
{ id: "56744723958ef13879b9549b", name: "Testing", slug: "testing" },
{ id: "56744721958ef13879b94e0c", name: "tools", slug: "tools" },
{
id: "56744723958ef13879b95372",
name: "debugging",
slug: "debugging",
},
{ id: "56744723958ef13879b9526c", name: "Git", slug: "git" },
{ id: "56744722958ef13879b94f96", name: "HTML", slug: "html" },
{ id: "584879f0c0aaf085e2012086", name: "Next.js", slug: "nextjs" },
{
id: "56744721958ef13879b94dc4",
name: "performance",
slug: "performance",
},
{
id: "56744721958ef13879b9495b",
name: "analytics",
slug: "analytics",
},
{ id: "56744723958ef13879b95245", name: "APIs", slug: "apis" },
{
id: "56744723958ef13879b95598",
name: "best practices",
tags_dictionary: {
JavaScript: { slug: "javascript", id: "56744721958ef13879b94cad" },
TypeScript: { slug: "typescript", id: "56744723958ef13879b954e0" },
Ubuntu: { slug: "ubuntu", id: "56744721958ef13879b94988" },
UI: { slug: "ui", id: "56744723958ef13879b954f5" },
"VS Code": { slug: "vscode", id: "57323a8bae9d49b5a5a5b39c" },
Windows: { slug: "windows", id: "56744723958ef13879b953f7" },
React: { slug: "reactjs", id: "56744723958ef13879b95434" },
Services: { slug: "services", id: "5682e64e2c29f7e0c86d024b" },
"Tailwind CSS": { slug: "tailwind-css", id: "5f4ebbb150b5c61ec6ef4ad2" },
Testing: { slug: "testing", id: "56744723958ef13879b9549b" },
Tools: { slug: "tools", id: "56744721958ef13879b94e0c" },
Debugging: { slug: "debugging", id: "56744723958ef13879b95372" },
Git: { slug: "git", id: "56744723958ef13879b9526c" },
HTML: { slug: "html", id: "56744722958ef13879b94f96" },
"Next.js": { slug: "nextjs", id: "584879f0c0aaf085e2012086" },
Performance: { slug: "performance", id: "56744721958ef13879b94dc4" },
Analytics: { slug: "analytics", id: "56744721958ef13879b9495b" },
APIs: { slug: "apis", id: "56744723958ef13879b95245" },
"Best Practices": {
slug: "best-practices",
id: "56744723958ef13879b95598",
},
{ id: "56744723958ef13879b953a7", name: "cli", slug: "cli" },
{
id: "56744722958ef13879b950eb",
name: "Databases",
slug: "databases",
},
{
id: "648b5554f9b78f110ed2c1eb",
name: "Shadcn UI",
slug: "shadcn-ui",
},
],
CLI: { slug: "cli", id: "56744723958ef13879b953a7" },
Databases: { slug: "databases", id: "56744722958ef13879b950eb" },
"Shadcn UI": { slug: "shadcn-ui", id: "648b5554f9b78f110ed2c1eb" },
},
},
medium: {
should_publish: true,
should_notify_followers: false,
// Search in https://medium.com/ the tag e.g. "Shadcn UI"
// When found, get it from the URL
// E.g. https://medium.com/tag/shadcn-ui -> shadcn-ui
tags_dictionary: [
"analytics",
"api",
"best-practices",
"cli",
"database",
"debugging",
"git",
"html",
"javaScript",
"nextjs",
"performance",
"react",
"services",
"tailwind-Css",
"testing",
"tools",
"typescript",
"ubuntu",
"ui",
"vscode",
"windows",
"shadcn-ui",
],
tags_dictionary: {
Analytics: { slug: "analytics" },
API: { slug: "api" },
"Best Practices": { slug: "best-practices" },
CLI: { slug: "cli" },
Database: { slug: "database" },
Debugging: { slug: "debugging" },
Git: { slug: "git" },
HTML: { slug: "html" },
JavaScript: { slug: "javaScript" },
"Next.js": { slug: "nextjs" },
Performance: { slug: "performance" },
React: { slug: "react" },
Services: { slug: "services" },
"Tailwind CSS": { slug: "tailwind-Css" },
Testing: { slug: "testing" },
Tools: { slug: "tools" },
TypeScript: { slug: "typescript" },
Ubuntu: { slug: "ubuntu" },
UI: { slug: "ui" },
"VS Code": { slug: "vscode" },
Windows: { slug: "windows" },
"Shadcn UI": { slug: "shadcn-ui" },
},
},
};

Expand Down
17 changes: 11 additions & 6 deletions src/clients/hashnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ type HashnodeTag = {
name: string;
};

type HashnodeTagDictionary = Record<string, { slug: string; id: string }>;

class HashnodeClient {
connection_settings: HashnodeConfigSchema["connection_settings"];
options: HashnodeConfigSchema["options"];
client: GraphQLClient;
postData: Post;
tagsDictionary: HashnodeTag[];
tagsDictionary: HashnodeTagDictionary;

constructor(config: HashnodeConfigSchema, postData: Post) {
this.connection_settings = config.connection_settings;
Expand All @@ -26,7 +28,7 @@ class HashnodeClient {
authorization: this.connection_settings.token,
},
});
this.tagsDictionary = config.options.tagsDictionary;
this.tagsDictionary = config.options.tags_dictionary;
}

// async createDictionary(username: string) {
Expand Down Expand Up @@ -63,11 +65,14 @@ class HashnodeClient {
private findTagInDictionary(queryTag: string): HashnodeTag {
// Very simple matching algorithm
const normalizedQuery = normalizeTag(queryTag);
const tag = this.tagsDictionary.find((tag) =>
normalizeTag(tag.slug).includes(normalizedQuery)
const entries = Object.entries(this.tagsDictionary);
const entry = entries.find(([name]) =>
normalizeTag(name).includes(normalizedQuery)
);
if (tag) {
return tag;

if (entry) {
const [name, { slug, id }] = entry;
return { name, slug, id };
}
throw Error(`Tag ${queryTag} not found in dictionary`);
}
Expand Down
15 changes: 9 additions & 6 deletions src/clients/medium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class MediumClient {
options: MediumConfigSchema["options"];
client: AxiosInstance;
postData: Post;
tagsDictionary: string[];
tagsDictionary: Record<string, { slug: string }>;

constructor(config: MediumConfigSchema, postData: Post) {
this.connection_settings = config.connection_settings;
Expand All @@ -21,7 +21,7 @@ class MediumClient {
Authorization: `Bearer ${this.connection_settings.token}`,
},
});
this.tagsDictionary = config.options.tagsDictionary;
this.tagsDictionary = config.options.tags_dictionary;
}

async post(dryRun?: boolean) {
Expand Down Expand Up @@ -84,11 +84,14 @@ class MediumClient {
private findTagInDictionary(queryTag: string): string {
// Very simple matching algorithm
const normalizedQuery = normalizeTag(queryTag);
const tag = this.tagsDictionary.find((tag) =>
normalizeTag(tag).includes(normalizedQuery)
const entries = Object.entries(this.tagsDictionary);
const entry = entries.find(([name]) =>
normalizeTag(name).includes(normalizedQuery)
);
if (tag) {
return tag;

if (entry) {
const [_, { slug }] = entry;
return slug;
}
throw Error(`Tag ${queryTag} not found in dictionary`);
}
Expand Down
16 changes: 11 additions & 5 deletions src/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,27 @@ const DevToOptionsSchema = z.object({
const HashnodeOptionsSchema = z.object({
should_hide: z.boolean().default(false),
tags_dictionary: z
.array(
.record(
z.object({
id: z.string(),
name: z.string(),
slug: z.string(),
id: z.string(),
})
)
.optional()
.default([]),
.default({}),
});

const MediumOptionsSchema = z.object({
should_publish: z.boolean().default(true),
should_notify_followers: z.boolean().default(false),
tags_dictionary: z.array(z.string()).optional().default([]),
tags_dictionary: z
.record(
z.object({
slug: z.string(),
})
)
.optional()
.default({}),
});

const DevToConfigSchema = z.object({
Expand Down

0 comments on commit 5c82b95

Please sign in to comment.