-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add meta tags endpoint to html to pdf (#789)
Signed-off-by: hxtree <[email protected]>
- Loading branch information
Showing
8 changed files
with
129 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// DO NOT MODIFY THIS FILE MANUALLY BUT DO COMMIT IT. It is generated and used by Rush. | ||
{ | ||
"pnpmShrinkwrapHash": "ef99d2be9bb5fde2af843e6f081457fe57fa679f", | ||
"pnpmShrinkwrapHash": "0f26d344f9b8b25f5850336a9f316858200a1459", | ||
"preferredVersionsHash": "a48003cf229dd47d077bcf6301ac15a6f90e1c34" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
services/html-to-pdf/src/module/meta-tags/meta-tags.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { | ||
Controller, Get, Param, Query, VERSION_NEUTRAL, | ||
} from '@nestjs/common'; | ||
import { MetaTagsService } from './meta-tags.service'; | ||
|
||
@Controller({ path: 'meta-tags', version: ['1', VERSION_NEUTRAL] }) | ||
export class MetaTagsController { | ||
constructor(private readonly metaTagsService: MetaTagsService) {} | ||
|
||
@Get() | ||
async getMetaTags( | ||
@Query('url') url: string, | ||
): Promise<{ [key: string]: string }> { | ||
return this.metaTagsService.getMetaTags(url); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
services/html-to-pdf/src/module/meta-tags/meta-tags.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { MetaTagsController } from './meta-tags.controller'; | ||
import { MetaTagsService } from './meta-tags.service'; | ||
|
||
@Module({ | ||
controllers: [MetaTagsController], | ||
providers: [MetaTagsService], | ||
}) | ||
export class MetaTagsModule {} |
36 changes: 36 additions & 0 deletions
36
services/html-to-pdf/src/module/meta-tags/meta-tags.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import axios from 'axios'; | ||
import * as cheerio from 'cheerio'; | ||
|
||
@Injectable() | ||
export class MetaTagsService { | ||
async getMetaTags(url: string): Promise<{ [key: string]: string }> { | ||
try { | ||
const response = await axios.get(url); | ||
const metaTags = this.extractMetaTags(response.data); | ||
return metaTags; | ||
} catch (err) { | ||
// Handle errors (e.g., network issues, invalid URLs) | ||
const error = err as Error; | ||
console.error('Error fetching or parsing the page:', error.message); | ||
throw new Error('Unable to fetch or parse the page'); | ||
} | ||
} | ||
|
||
private extractMetaTags(html: string): { [key: string]: string } { | ||
const $ = cheerio.load(html); | ||
const metaTags: { [key: string]: string } = {}; | ||
|
||
$('meta').each((_, element) => { | ||
const tag = $(element); | ||
const name = tag.attr('name') || tag.attr('property'); | ||
const content = tag.attr('content'); | ||
|
||
if (name && content) { | ||
metaTags[name] = content; | ||
} | ||
}); | ||
|
||
return metaTags; | ||
} | ||
} |