Skip to content

Commit

Permalink
chore: treat "fb:" as "og:"
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Oct 18, 2024
1 parent 4508f32 commit 1770360
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ const addBody = ({ url, headers, html }) => {
return `<!DOCTYPE html><html><head></head><body>${element}</body></html>`
}

const isOpenGraph = (prop = '') =>
['og:', 'fb:'].some(prefix => prop.startsWith(prefix))

const rewriteMetaTags = ({ $ }) => {
$('meta').each((_, element) => {
const el = $(element)
Expand All @@ -99,11 +102,11 @@ const rewriteMetaTags = ({ $ }) => {
const property = el.attr('property')

// Convert 'name' to 'property' for Open Graph tags if 'property' is not already set correctly
if (name?.startsWith('og:') && property !== name) {
if (isOpenGraph(name) && property !== name) {
el.removeAttr('name').attr('property', name)
debug('og', el.attr())
// Convert 'property' to 'name' for non-Open Graph tags
} else if (property && !property.startsWith('og')) {
} else if (!isOpenGraph(property)) {
el.removeAttr('property').attr('name', property)
debug('meta', el.attr())
}
Expand Down
27 changes: 27 additions & 0 deletions test/html/rewrite-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,33 @@ test("don't rewrite og if property is already present", async t => {
)
})

test('fb propietary tags should be treat as og', async t => {
{
const output = html({
rewriteHtml: true,
url: 'https://kikobeats.com',
html: composeHtml(['<meta content="1234" property="fb:app_id">']),
headers: { 'content-type': 'text/html; charset=utf-8' }
})

const $ = cheerio.load(output)
t.is($('meta[property="fb:app_id"]').attr('content'), '1234')
t.is($('meta[name="fb:app_id"]').attr('content'), undefined)
}
{
const output = html({
rewriteHtml: true,
url: 'https://kikobeats.com',
html: composeHtml(['<meta content="1234" name="fb:app_id">']),
headers: { 'content-type': 'text/html; charset=utf-8' }
})

const $ = cheerio.load(output)
t.is($('meta[property="fb:app_id"]').attr('content'), '1234')
t.is($('meta[name="fb:app_id"]').attr('content'), undefined)
}
})

test("don't rewrite og if content is empty", async t => {
const output = html({
rewriteHtml: true,
Expand Down

0 comments on commit 1770360

Please sign in to comment.