From 613b8a33ece65d896c3e897178c74ab6883090a3 Mon Sep 17 00:00:00 2001 From: curben <2809763-curben@users.noreply.gitlab.com> Date: Wed, 4 Sep 2019 03:36:08 +0100 Subject: [PATCH] refactor(copy-button): replace cheerio with regex --- README.md | 5 ++--- scripts/copy-button.js | 14 +++++--------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 6d44d0a..46de46d 100644 --- a/README.md +++ b/README.md @@ -31,13 +31,12 @@ The following are the major changes I made compared to the upstream. ### Plugins - Installed [hexo-nofollow](https://github.com/curbengh/hexo-nofollow) for SEO purpose to prevent search engines from following external links. - Installed [hexo-yam](https://github.com/curbengh/hexo-yam) to pre-compress static assets (html, css, js and svg). -- [cloudinary.js](/scripts/cloudinary.js) - A [tag](https://hexo.io/api/tag) plugin to easily embed [Cloudinary](https://cloudinary.com/) in a post. - [copy-button.js](/scripts/copy-button.js) - A [filter](https://hexo.io/api/filter) plugin to add a copy button to each code block. - [feed](/scripts/feed) - A generator plugin to generate [an RSS feed](https://en.wikipedia.org/wiki/Web_feed). Modified from [hexo-generator-feed](https://github.com/hexojs/hexo-generator-feed) to use `post.lastUpdated` instead of `post.updated`. - [heading-link.js](/scripts/heading-link.js) - A filter plugin to add a link button next to each heading. +- [image.js](/scripts/image.js) - A [tag](https://hexo.io/api/tag) plugin to easily embed images in a post with responsive image support. - [link.js](/scripts/link.js) - A [helper](https://hexo.io/api/helper) plugin to add a link. Modified from [link_to.js](https://github.com/hexojs/hexo/blob/master/lib/plugins/helper/link_to.js) to remove title attribute and 'external' option. -- [listTags](/scripts/listTags.js) - A helper plugin to display tags. Modified from [link_tags.js](https://github.com/hexojs/hexo/blob/master/lib/plugins/helper/link_tags.js) to add [rel="tag"](http://microformats.org/wiki/rel-tag) and [itemprop="keywords"](https://schema.org/keywords) attributes. -- [openGraph](/scripts/openGraph.js) - A helper plugin to add [Open Graph](https://ogp.me/) tags. Modified from [open_graph.js](https://github.com/hexojs/hexo/blob/master/lib/plugins/helper/open_graph.js) to be compatible with "cloudinary.js". +- [openGraph](/scripts/openGraph.js) - A helper plugin to add [Open Graph](https://ogp.me/) tags. Modified from [open_graph.js](https://github.com/hexojs/hexo/blob/master/lib/plugins/helper/open_graph.js) to use additional [Open Graph](https://www.ogp.me/) tags and [WHATWG URL API](https://nodejs.org/api/url.html#url_the_whatwg_url_api). - [sitemap](/scripts/sitemap) - A [generator](https://hexo.io/api/generator) plugin to generate [a sitemap](https://en.wikipedia.org/wiki/Sitemaps). Modified from [hexo-generator-sitemap](https://github.com/hexojs/hexo-generator-sitemap) with this [patch](https://github.com/hexojs/hexo-generator-sitemap/pull/26) to include tags/categories and [remove](https://github.com/pyyzcwg2833/hexo-generator-sitemap/commit/a92dbbb83cc39ff60d43faa5cd688a56574a3889) [index.html](https://github.com/hexojs/hexo-generator-sitemap/pull/59) from the URL. ## License diff --git a/scripts/copy-button.js b/scripts/copy-button.js index 78301f5..0a709ca 100644 --- a/scripts/copy-button.js +++ b/scripts/copy-button.js @@ -3,18 +3,14 @@ /* * Add "Copy" button to code snippet -* cheerio is provided by hexo package */ -const cheerio = require('cheerio') - -hexo.extend.filter.register('after_render:html', (str) => { - const $ = cheerio.load(str) - +hexo.extend.filter.register('after_render:html', (data) => { // Avoid duplicate button - if ($('button').parent().hasClass('code')) return + if (data.includes('')) return; - $('.code').append('') + const copyBtn = '' - return $.html() + // Regex is based on https://github.com/hexojs/hexo/pull/3697 + return data.replace(/(?!<\/td>).+?<\/td>/, (str) => str.replace('', copyBtn + '')) })