Skip to content

Commit

Permalink
feat: add ogTags
Browse files Browse the repository at this point in the history
  • Loading branch information
Wojciech Krysiak authored and Wojciech Krysiak committed Sep 7, 2020
1 parent 418fdf7 commit 17a505d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 9 deletions.
1 change: 1 addition & 0 deletions fixtures/typescript/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { RecordError } from '../adapters/base-record'

/**
* Error which is thrown when there are validation errors with records
* @ogTitle Some title from tag
* @category Errors
*/
class ValidationError extends Error {
Expand Down
5 changes: 4 additions & 1 deletion jsdoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
"templates": {
"better-docs": {
"name": "AdminBro Documentation",
"logo": "images/logo.png"
"logo": "images/logo.png",
"ogTags": {
"title": "some title"
}
}
}
}
17 changes: 14 additions & 3 deletions navigation.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
const availableOgTags = ['image', 'title', 'description', 'url', 'type', 'locale']

exports.defineTags = (dictionary) => {
dictionary.defineTag('category', {
onTagged: (doclet, tag) => {
doclet.category = tag.value
}
},
})
dictionary.defineTag('subcategory', {
onTagged: (doclet, tag) => {
doclet.subCategory = tag.value
}
},
})
dictionary.defineTag('section', {
onTagged: (doclet, tag) => {
doclet.section = tag.value
}
},
})

availableOgTags.forEach((tagName) => {
dictionary.defineTag(`og${tagName.toUpperCase()}`, {
onTagged: (doclet, tag) => {
doclet.ogTags = doclet.ogTags || {}
doclet.ogTags[tagName] = tag.value
},
})
})
}
12 changes: 8 additions & 4 deletions src/better-docs.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ export type NavLink = {

export type OgTags = {
/**
* @ogTitle
* @ogDescription
* @ogImage
* Default value for @ogTitle tag
*/
title?: string;
/**
* Default value for @ogDescription tags
*/
title: string;
description: string;
/**
* Default value for @ogImage tags
*/
image: string;
}

Expand Down
11 changes: 11 additions & 0 deletions tmpl/head.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<?js
self = this
let tags = ['image', 'title', 'description', 'url', 'type', 'locale']
let ogTags = {}

tags.forEach(function (tag) {
ogTags[tag] = (doc && doc.ogTags && doc.ogTags[tag] || betterDocs.ogTags && betterDocs.ogTags[tag])
})
?>
<head>
<?js= betterDocs.trackingCode ?>
Expand All @@ -25,5 +31,10 @@
<?js if (betterDocs.head) { ?>
<?js= betterDocs.head ?>
<?js } ?>
<?js Object.keys(ogTags).forEach( function(tagName) { ?>
<?js if (ogTags[tagName]) { ?>
<meta property="og:<?js= tagName ?>" content="<?js= ogTags[tagName] ?>" />
<?js } ?>
<?js }) ?>
</head>

2 changes: 1 addition & 1 deletion tmpl/layout.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var search = env.conf.templates && env.conf.templates.search

<!DOCTYPE html>
<html lang="en">
<?js= this.partial('head.tmpl', { betterDocs, title }) ?>
<?js= this.partial('head.tmpl', { betterDocs, title, doc }) ?>

<body class="<?js= self.smallHeader ? 'layout small-header' : 'layout'?>">
<div id="stickyNavbarOverlay"></div>
Expand Down

0 comments on commit 17a505d

Please sign in to comment.