From 10c0a7296d0f0ca13a250e797c7cc9ae74e189be Mon Sep 17 00:00:00 2001 From: Oscar Date: Thu, 11 Jun 2020 19:12:58 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=85=20Update=20ESLint=20scope=20(#204)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * lint: let eslint lint all js files in project && auto-fix * lint: ignore dist, unignore eleventy config * del unused minified file * 💅 --- .eleventy.js | 312 +++++++++++++------------- .eslintignore | 2 + _util/create-empty-definitions-min.js | 1 - _util/create-empty-definitions.js | 37 +-- package.json | 4 +- 5 files changed, 187 insertions(+), 169 deletions(-) create mode 100644 .eslintignore delete mode 100644 _util/create-empty-definitions-min.js diff --git a/.eleventy.js b/.eleventy.js index 1e0da1ad..037b5b38 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -1,181 +1,193 @@ -const definitionPermalink = require('./11ty/helpers/definitionPermalink') -const renderDefinitionContentNextEntries = require('./11ty/shortcodes/renderDefinitionContentNextEntries') -const findExistingDefinition = require('./11ty/filters/helpers/findExistingDefinition') +const definitionPermalink = require('./11ty/helpers/definitionPermalink'); +const renderDefinitionContentNextEntries = require('./11ty/shortcodes/renderDefinitionContentNextEntries'); +const findExistingDefinition = require('./11ty/filters/helpers/findExistingDefinition'); module.exports = function(config) { - // Add a filter using the Config API - config.addFilter('linkTarget', definitionPermalink) + // Add a filter using the Config API + config.addFilter('linkTarget', definitionPermalink); - config.addFilter('linkIfExistsInCollection', (word, collection) => { - const existingDefinition = findExistingDefinition(word, collection) + config.addFilter('linkIfExistsInCollection', (word, collection) => { + const existingDefinition = findExistingDefinition(word, collection); - if (existingDefinition) { - return `${word}` - } + if (existingDefinition) { + return `${word}`; + } - return `${word}` - }) + return `${word}`; + }); - config.addFilter('linkSubTermIfDefined', (subTermData, collection) => { - const existingDefinition = findExistingDefinition(subTermData.full_title, collection) + config.addFilter('linkSubTermIfDefined', (subTermData, collection) => { + const existingDefinition = findExistingDefinition( + subTermData.full_title, + collection + ); - if (existingDefinition) { - return `${subTermData.text}` - } + if (existingDefinition) { + return `${ + subTermData.text + }`; + } - return `${subTermData.text}` - }) + return `${subTermData.text}`; + }); - // just a debug filter to lazily inspect the content of anything in a template - config.addFilter('postInspect', function(post) { - console.log(post) - }) + // just a debug filter to lazily inspect the content of anything in a template + config.addFilter('postInspect', function(post) { + console.log(post); + }); - config.addShortcode('definitionFlag', (flag) => { - const cleanText = new Map([ - [ - 'avoid', - { - class: 'avoid', - text: 'Avoid' - } - ], - [ - 'better-alternative', - { - class: 'better', - text: 'Better alternate' - } - ], - [ - 'tool', - { - class: 'tool', - text: '' - } - ], - [ - 'warning', - { - class: 'warning', - text: '' - } - ] - ]) + config.addShortcode('definitionFlag', (flag) => { + const cleanText = new Map([ + [ + 'avoid', + { + class: 'avoid', + text: 'Avoid' + } + ], + [ + 'better-alternative', + { + class: 'better', + text: 'Better alternate' + } + ], + [ + 'tool', + { + class: 'tool', + text: '' + } + ], + [ + 'warning', + { + class: 'warning', + text: '' + } + ] + ]); - if (flag) { - const info = cleanText.get(flag.level.toLowerCase()) + if (flag) { + const info = cleanText.get(flag.level.toLowerCase()); - const sep = flag.text && info.text ? '—' : '' - const text = flag.text ? [ info.text, flag.text ].join(sep) : info.text + const sep = flag.text && info.text ? '—' : ''; + const text = flag.text ? [info.text, flag.text].join(sep) : info.text; - return `

${text}

` - } + return `

${text}

`; + } - return '

' - }) + return '

'; + }); - config.addShortcode('renderDefinitionContentNextEntries', renderDefinitionContentNextEntries) + config.addShortcode( + 'renderDefinitionContentNextEntries', + renderDefinitionContentNextEntries + ); - // NOTE (ovlb): this will not be remembered as the best code i’ve written. if anyone seeing this has a better solution then the following to achieve sub groups of the definitions: i am happy to get rid of it - config.addCollection('tableOfContent', (collection) => { - const allItems = collection - .getFilteredByGlob('./11ty/definitions/*.md') - .filter((word) => !word.data.skip_in_table_of_content) - .sort((a, b) => { - const { title: firstTitle } = a.data - const { title: secondTitle } = b.data - const sortA = firstTitle.toLowerCase().replace(/^-/, '') - const sortB = secondTitle.toLowerCase().replace(/^-/, '') + // NOTE (ovlb): this will not be remembered as the best code i’ve written. if anyone seeing this has a better solution then the following to achieve sub groups of the definitions: i am happy to get rid of it + config.addCollection('tableOfContent', (collection) => { + const allItems = collection + .getFilteredByGlob('./11ty/definitions/*.md') + .filter((word) => !word.data.skip_in_table_of_content) + .sort((a, b) => { + const { title: firstTitle } = a.data; + const { title: secondTitle } = b.data; + const sortA = firstTitle.toLowerCase().replace(/^-/, ''); + const sortB = secondTitle.toLowerCase().replace(/^-/, ''); - // `localeCompare()` is super cool: http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare - return sortA.localeCompare(sortB) - }) + // `localeCompare()` is super cool: http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare + return sortA.localeCompare(sortB); + }); - const split = { - notLetters: { - title: '#', - definitions: [] - }, - aToE: { - title: 'A–E', - definitions: [] - }, - fToL: { - title: 'F–L', - definitions: [] - }, - mToS: { - title: 'M–S', - definitions: [] - }, - tToZ: { - title: 'T–Z', - definitions: [] - } - } + const split = { + notLetters: { + title: '#', + definitions: [] + }, + aToE: { + title: 'A–E', + definitions: [] + }, + fToL: { + title: 'F–L', + definitions: [] + }, + mToS: { + title: 'M–S', + definitions: [] + }, + tToZ: { + title: 'T–Z', + definitions: [] + } + }; - allItems.forEach((word) => { - const { title } = word.data - const { notLetters, aToE, fToL, mToS, tToZ } = split - const sortableTitle = title.replace(/^-/, '') + allItems.forEach((word) => { + const { title } = word.data; + const { notLetters, aToE, fToL, mToS, tToZ } = split; + const sortableTitle = title.replace(/^-/, ''); - if (/^[a-e]/gim.test(sortableTitle)) { - return aToE.definitions.push(word) - } + if (/^[a-e]/gim.test(sortableTitle)) { + return aToE.definitions.push(word); + } - if (/^[f-l]/i.test(sortableTitle)) { - return fToL.definitions.push(word) - } + if (/^[f-l]/i.test(sortableTitle)) { + return fToL.definitions.push(word); + } - if (/^[m-s]/i.test(sortableTitle)) { - return mToS.definitions.push(word) - } + if (/^[m-s]/i.test(sortableTitle)) { + return mToS.definitions.push(word); + } - if (/^[t-z]/i.test(sortableTitle)) { - return tToZ.definitions.push(word) - } + if (/^[t-z]/i.test(sortableTitle)) { + return tToZ.definitions.push(word); + } - // no reg ex as the fallback to avoid testing for emojis and numbers - notLetters.definitions.push(word) - }) + // no reg ex as the fallback to avoid testing for emojis and numbers + notLetters.definitions.push(word); + }); - return Object.keys(split).map((key) => { - const { title, definitions } = split[key] + return Object.keys(split).map((key) => { + const { title, definitions } = split[key]; - return { title, definitions } - }) - }) + return { title, definitions }; + }); + }); - config.addCollection('definedWords', (collection) => { - return collection - .getFilteredByGlob('./11ty/definitions/*.md') - .filter((word) => word.data.defined) - .sort((a, b) => { - // `localeCompare()` is super cool: http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare - return a.data.title.toLowerCase().localeCompare(b.data.title.toLowerCase()) - }) - }) + config.addCollection('definedWords', (collection) => { + return collection + .getFilteredByGlob('./11ty/definitions/*.md') + .filter((word) => word.data.defined) + .sort((a, b) => { + // `localeCompare()` is super cool: http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare + return a.data.title + .toLowerCase() + .localeCompare(b.data.title.toLowerCase()); + }); + }); - const mdIt = require('markdown-it')({ - html: true - }) - const prism = require('markdown-it-prism') - const anchor = require('markdown-it-anchor') + const mdIt = require('markdown-it')({ + html: true + }); + const prism = require('markdown-it-prism'); + const anchor = require('markdown-it-anchor'); - mdIt.use(prism) - mdIt.use(anchor) + mdIt.use(prism); + mdIt.use(anchor); - config.setLibrary('md', mdIt) + config.setLibrary('md', mdIt); - // You can return your Config object (optional). - return { - dir: { - input: '11ty', - output: 'dist' - }, - templateFormats: [ 'njk', 'md' ], - htmlTemplateEngine: 'njk', - markdownTemplateEngine: 'njk' - } -} + // You can return your Config object (optional). + return { + dir: { + input: '11ty', + output: 'dist' + }, + templateFormats: ['njk', 'md'], + htmlTemplateEngine: 'njk', + markdownTemplateEngine: 'njk' + }; +}; diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 00000000..2369e454 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,2 @@ +dist/ +!.eleventy.js diff --git a/_util/create-empty-definitions-min.js b/_util/create-empty-definitions-min.js deleted file mode 100644 index bb543c16..00000000 --- a/_util/create-empty-definitions-min.js +++ /dev/null @@ -1 +0,0 @@ -import slugify from"slugify";import path from"path";import fs from"fs";import{promisify}from"util";const writeFile=promisify(fs.writeFile);import{words}from"./undefined-words";const defintionPath=path.resolve(process.cwd(),"11ty/definitions/"),template="\n---\ntitle: {{title}}\nslug: {{slug}}\ndefined: false\n---\n";export function createDefinitions(){return words.forEach(async t=>{const e=t,i=slugify(t.toLowerCase().replace(/ \([a-z]+\)| ([a-z-]+) slur/i,"")),r=template.replace("{{title}}",e).replace("{{slug}}",i).trim();try{await writeFile(`${defintionPath}/${i}.md`,r,"utf8")}catch(t){console.error(t),process.exit(1)}return!0})} \ No newline at end of file diff --git a/_util/create-empty-definitions.js b/_util/create-empty-definitions.js index 8afe85a3..15f9897b 100644 --- a/_util/create-empty-definitions.js +++ b/_util/create-empty-definitions.js @@ -1,12 +1,12 @@ -import slugify from 'slugify' -import path from 'path' -import fs from 'fs' -import { promisify } from 'util' +import slugify from 'slugify'; +import path from 'path'; +import fs from 'fs'; +import { promisify } from 'util'; -const writeFile = promisify(fs.writeFile) +const writeFile = promisify(fs.writeFile); -import { words } from './undefined-words' -const defintionPath = path.resolve(process.cwd(), '11ty/definitions/') +import { words } from './undefined-words'; +const defintionPath = path.resolve(process.cwd(), '11ty/definitions/'); const template = ` --- @@ -14,23 +14,28 @@ title: {{title}} slug: {{slug}} defined: false --- -` +`; export function createDefinitions() { return words.forEach(async (word) => { - const title = word - const slug = slugify(word.toLowerCase().replace(/ \([a-z]+\)| ([a-z-]+) slur/i, '')) + const title = word; + const slug = slugify( + word.toLowerCase().replace(/ \([a-z]+\)| ([a-z-]+) slur/i, '') + ); - const content = template.replace('{{title}}', title).replace('{{slug}}', slug).trim() + const content = template + .replace('{{title}}', title) + .replace('{{slug}}', slug) + .trim(); try { - await writeFile(`${defintionPath}/${slug}.md`, content, 'utf8') + await writeFile(`${defintionPath}/${slug}.md`, content, 'utf8'); } catch (e) { - console.error(e) + console.error(e); - process.exit(1) + process.exit(1); } - return true - }) + return true; + }); } diff --git a/package.json b/package.json index 249d9bee..dc6eaea6 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,8 @@ "build:site": "eleventy", "create-definitions": "bin/create-definitions", "lint": "concurrently npm:lint:*", - "lint:css": "stylelint \"assets/css/**/*.scss\"", - "lint:js": "eslint \"11ty/**/*.js\"", + "lint:css": "stylelint \"assets/css/**/*.scss\" --fix", + "lint:js": "eslint \"**/*.js\" --fix", "lint:markdown": "markdownlint '**/*.md' --ignore node_modules", "serve": "concurrently \"npm run serve:site\" \"npm run watch:css\"", "serve:site": "eleventy --serve",