Skip to content

Fix #3: handle og:image meta tag #4

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,4 @@ dist

# TernJS port file
.tern-port
.DS_Store
37 changes: 27 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,44 @@
const fetch = require('node-fetch'),
express = require('express'),
app = express()
express = require('express'),
app = express()

const parse = (body) => {
let match = body.match(/<title>([^<]*)<\/title>/) // regular expression to parse contents of the <title> tag
if (!match || typeof match[1] !== 'string')
// fontion parse :
// attendus : doit renvoyer un json contenant les éléments title et image
// comment tester : vérifier que c'est du json, que title est du texte, ..
const parse = body => {
let matchTitle = body.match(/<title>([^<]*)<\/title>/) // regular expression to parse contents of the <title> tag
let matchImage = body.match(/([a-z\-_0-9\/\:\.]*\.(jpg|jpeg|png))/i) // regular expression to parse contents of the <meta> tag, NB: not working for other path than http.. https://regex101.com/r/rX1zK7/1
if (!matchTitle || typeof matchTitle[1] !== 'string')
throw new Error('Unable to parse the title tag')
return {
title: match[1],
image: "TODO"
if (!matchImage) {
throw new Error('Unable to parse the meta tag')
}
else return {
title: matchTitle[1],
image: matchImage[0]
}
}

app.use(function (req, res, next) {
res.setHeader(
'Content-Security-Policy-Report-Only',
"default-src 'self'; font-src 'self'; img-src 'self'; script-src 'self'; style-src 'self'; frame-src 'self'"
);
next();
});

app.get('/', (req, res) => {
const { url } = req.query
if (!url)
return res.status(400).end('Missing url query parameter')

fetch(url)
.then(res => res.text()) // parse response's body as text
.then(body => parse(body)) // extract infos from body
.then(json => res.send(json)) // send the result back
.then(json => res.json(json)) // send the result back
.catch(e => res.status(500).end(e.message)) // catch possible errors
})

app.listen(80)

// query /tblOT7RAdbNGccvtG?fields%5B%5D=Titre+de+la+formation&fields%5B%5D=Organisme&maxRecords=100&sort%5B0%5D%5Bfield%5D=Titre+de+la+formation
Loading