Skip to content

Instantly share code, notes, and snippets.

@dbwodlf3
Last active September 22, 2022 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbwodlf3/172d25840094f4b60b5268d57a44e7f7 to your computer and use it in GitHub Desktop.
Save dbwodlf3/172d25840094f4b60b5268d57a44e7f7 to your computer and use it in GitHub Desktop.
parse opengraph
/** Parsing only meta in headtag. */
export function parse_opengraph(inputHTML) {
const filter_head = /<head>[^]*<\/head>/i;
let filter_meta_og = /<meta[^>]*property[^>]*=[^>]*(og:)[^>]*>/ig;
let head = inputHTML.match(filter_head)[0];
const metas = head.match(filter_meta_og);
if(!metas) return;
const opengraph = {}
for(const meta of metas) {
opengraph[`${(meta.match(/og\s*:([a-zA-Z]*)/)[1])}`] = meta.match(/content[^=]*="(.*)"|'(.*)'/)[1];
}
return opengraph;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment