From 5a65349831fd2a72b6ae3668db4fd299041b910b Mon Sep 17 00:00:00 2001 From: Ashley Williams Date: Mon, 31 Oct 2016 15:53:28 -0400 Subject: [PATCH] separate packagize code into separate dep, pkgize --- lib/plugin/packagize.js | 72 ----------------------------------------- lib/render.js | 2 +- package.json | 1 + 3 files changed, 2 insertions(+), 73 deletions(-) delete mode 100644 lib/plugin/packagize.js diff --git a/lib/plugin/packagize.js b/lib/plugin/packagize.js deleted file mode 100644 index 75f7f51..0000000 --- a/lib/plugin/packagize.js +++ /dev/null @@ -1,72 +0,0 @@ -// markdown-it plugin to add some helper classes to existing elements that look -// like duplicates of package names and descriptions -// -var similarity = require('similarity') -var tokenUtil = require('../token-util') - -module.exports = function (md, opts) { - if (!opts) return - if (!opts.package) return - if (!opts.package.name) return - - var packageName = opts.package.name - var packageDescription = opts.package.description + '' // invalid description in some cases can cause a crash - - // - // process the first

element - // - var originalHeadingRule = md.renderer.rules.heading_open - md.renderer.rules.heading_open = function (tokens, idx, options, env, self) { - if (tokens[idx].tag === 'h1' && !env.packagizeHeadingDone) { - // extract the text from the heading's 'inline' token - var text = tokenUtil.getText(tokens[idx + 1]) - - // check to see if the heading text matches the package name - if ( - similarity(packageName, text) > 0.6 || - similarity(packageName.replace(/^@[^\/]+\//, ''), text) > 0.6 || // filter out scope name - ~text.toLowerCase().indexOf(packageName.toLowerCase()) - ) { - tokens[idx].attrJoin('class', 'package-name-redundant') - } - - // check to see if the heading text matches the package description - if (packageDescription && ( - similarity(packageDescription, text) > 0.6 || - ~text.toLowerCase().indexOf(packageDescription.toLowerCase()) - )) { - tokens[idx].attrJoin('class', 'package-description-redundant') - } - - // only inspect the first h1; skip the rest - env.packagizeHeadingDone = true - } - return originalHeadingRule.call(this, tokens, idx, options, env, self) - } - - // - // process the first paragraph that contains text - // - var originalParagraphRule = md.renderer.rules.paragraph_open - md.renderer.rules.paragraph_open = function (tokens, idx, options, env, self) { - if (!env.packagizeParagraphDone) { - // extract the text from the paragraph's 'inline' token - var text = tokenUtil.getText(tokens[idx + 1]) - - // check to see if the paragraph text matches the description - if (similarity(packageDescription, text) > 0.6) { - tokens[idx].attrJoin('class', 'package-description-redundant') - } - - // inspect the first paragraph that contains text; skip the rest - if (text) { - env.packagizeParagraphDone = true - } - } - if (originalParagraphRule) { - return originalParagraphRule.call(this, tokens, idx, options, env, self) - } else { - return md.renderer.renderToken(tokens, idx, options) - } - } -} diff --git a/lib/render.js b/lib/render.js index ef09457..5765276 100644 --- a/lib/render.js +++ b/lib/render.js @@ -15,7 +15,7 @@ var github = require('./plugin/github') var youtube = require('./plugin/youtube') var cdnImages = require('./plugin/cdn') var badges = require('./plugin/badges') -var packagize = require('./plugin/packagize') +var packagize = require('pkgize') var relaxedLinkRefs = require('./gfm/relaxed-link-reference') var githubHeadings = require('./gfm/indented-headings') diff --git a/package.json b/package.json index e302663..9908128 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "markdown-it-expand-tabs": "^1.0.7", "markdown-it-lazy-headers": "^0.1.3", "markdown-it-task-lists": "^1.0.0", + "pkgize": "^0.1.1", "property-ttl": "^1.0.0", "sanitize-html": "^1.6.1", "similarity": "^1.0.1"