Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any reason not to include data fetching logic? #13

Open
planetbk9 opened this issue Aug 13, 2019 · 3 comments
Open

Is there any reason not to include data fetching logic? #13

planetbk9 opened this issue Aug 13, 2019 · 3 comments

Comments

@planetbk9
Copy link

I think that extracting meta data from url could be implemented in this extension.
But according to the guide, users should implement that logic in their own server.
I am just curious that there is some reason like security or something.

@hboylan
Copy link

hboylan commented Feb 6, 2020

I had the same thought, but it turns out CORs will prevent one site from requesting the HTML from another. It's pretty easy to create an endpoint for this anyway. Here's my implementation with NestJS.

link.controller.ts

import { Controller, Get, Query } from '@nestjs/common'
import { LinkService } from 'src/link/link.service'

@Controller('link')
export class LinkController {
  constructor(private readonly linkService: LinkService) {}

  @Get()
  link(@Query('url') url: string) {
    return this.linkService.getMetadata(url)
  }
}

link.service.ts

import { Injectable, HttpService } from '@nestjs/common'
import { getMetadata } from 'page-metadata-parser'
import domino from 'domino'

@Injectable()
export class LinkService {
  constructor(private readonly httpService: HttpService) {}

  async getMetadata(url: string) {
    const res = await this.httpService.get(url).toPromise()
    const doc = domino.createWindow(res.data).document
    const meta = getMetadata(doc, url)
    return {
      success: 1,
      meta: {
        ...meta,
        image: {
          url: meta.image ?? meta.icon ?? '',
        },
      },
    }
  }
}

@C-Hodor
Copy link

C-Hodor commented Nov 16, 2020

I am using editorjs with PHP and I have no idea of how I should do this at the moment.

Having some examples would be of real help, indeed.

@drj-sharma
Copy link

drj-sharma commented Dec 5, 2020

My nodejs api query:



// cheerio is used for faster implementation
// https://www.npmjs.com/package/cheerio
// editorjs link previewer
app.get("/fetchUrl", async (req, res) => {
  let url = req.query.url;
  let resHTML = await fetch(new URL(url))
                      .catch((e) => console.log(e));
  const html = await resHTML.text();
  const $ = cheerio.load(html);

  // custom meta-tag function
  const getMetaTag = (value) => {
    return $(`meta[name=${value}]`).attr("content") ||
           $(`meta[property="og:${value}"]`).attr("content") ||
           $(`meta[property="twitter:${value}"]`).attr("content")
  }

  const resi = {
    success: 1,
    meta: {
      title: $("title").first().text(),
      description: getMetaTag("description"),
      image: {
        url: getMetaTag("image")
      }
    },
  };
  res.send(resi);
  return;
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants