Skip to content

Commit

Permalink
fix: ignore nested title tags (#76)
Browse files Browse the repository at this point in the history
fixes #75
  • Loading branch information
jacktuck committed May 26, 2021
1 parent 915bf8a commit 0887137
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ function getMetadata (ctx, opts: Opts) {

return new Promise(resolve => {
const parser: any = new Parser({
_nodes_from_root: 0,

onend: function () {
if (this._favicon === undefined) {
metadata.push(['favicon', resolveUrl(ctx.url, '/favicon.ico')])
Expand Down Expand Up @@ -231,6 +233,8 @@ function getMetadata (ctx, opts: Opts) {
},

onopentag: function (tagname, attribs) {
this._nodes_from_root++

if (opts.oembed && attribs.href) {
// handle XML and JSON with a preference towards JSON since its more efficient for us
if (
Expand Down Expand Up @@ -271,9 +275,10 @@ function getMetadata (ctx, opts: Opts) {
},

onclosetag: function (tag) {
this._nodes_from_root--
this._tagname = ''

if (tag === 'title') {
if (this._nodes_from_root <= 2 && tag === 'title') {
metadata.push(['title', this._title])
this._title = ''
}
Expand Down
12 changes: 12 additions & 0 deletions test/basic/basic-body.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
<title>ccc</title>
<meta name="description" content="aaa" />
<meta name="keywords" content="a, b, c" />

<svg viewBox="0 0 20 10" xmlns="http://www.w3.org/2000/svg">
<title>I'm a SVG</title>

<circle cx="5" cy="5" r="4">
<title>I'm a circle</title>
</circle>

<rect x="11" y="1" width="8" height="8">
<title>I'm a square</title>
</rect>
</svg>
</body>
</html>

Expand Down

0 comments on commit 0887137

Please sign in to comment.