diff --git a/.eslintrc.js b/.eslintrc.js index 668fac4..818fa85 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -3,15 +3,11 @@ module.exports = { es6: true, node: true, }, - extends: [ - 'google', - ], parserOptions: { ecmaVersion: 2018, sourceType: 'module', }, - rules: { - 'arrow-parens': ['error', 'as-needed'], - 'indent': ['error', 2, {'MemberExpression': 1}], - }, + extends: [ + '@sup39/basic', + ], }; diff --git a/lib/index.js b/lib/index.js index 98fb885..7e3fe4c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -2,8 +2,10 @@ const {parseAttrs, attrConcat} = require('./utils.js'); const {inlineAttrsApplyRules} = require('./rules.js'); /** - * @param {MarkdownIt} md - * @param {{deliminator: string, re: RegExp}} opts + * @type {import('markdown-it').PluginWithOptions<{ + * deliminator?: string, + * re?: RegExp, + * }>} */ function pluginAttr(md, opts={}) { const deliminator = opts.deliminator || '{'; diff --git a/lib/rules.js b/lib/rules.js index a7433b0..d05d1e4 100644 --- a/lib/rules.js +++ b/lib/rules.js @@ -1,7 +1,17 @@ +/** + * @typedef {import('markdown-it/lib/token')} Token + */ const { findOpenToken, find, rfind, rfindIndex, attrConcat, } = require('./utils.js'); +/** + * @type {{ + * name: string + * disabled?: boolean + * handler(token: Token, i: number, tokens: Token[], iBlock: number, blockTokens: Token[]): void + * }[]} + */ const inlineAttrsApplyRules = [ { name: 'first_child', @@ -22,9 +32,9 @@ const inlineAttrsApplyRules = [ }, { name: 'after_inline_block', - handler(token, i, tokens, iBlock, blockTokens) { + handler(token, i, tokens) { // find non-empty text token - const iC = rfindIndex(tokens, i-1, t=>t.type!=='text'||t.content); + const iC = rfindIndex(tokens, i-1, t=>t.type!=='text'||!!t.content); const tokenC = iC>=0 && tokens[iC]; if (!tokenC) return; // check tokenC type diff --git a/lib/utils.js b/lib/utils.js index b6d167c..f26a66a 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,8 +1,12 @@ +/** + * @typedef {import('markdown-it/lib/token')} Token + */ + /** * @param {string} s - input string * @param {RegExp} re - regular expression for attributes * @param {number} [limit=65536] - limit of attribute count - * @return {{attr: [string, string][]}|null} + * @return {{attrs: [string, string][], index: number}|null} */ function parseAttrs(s, re, limit=65536) { let count = 0; @@ -30,7 +34,7 @@ function parseAttrs(s, re, limit=65536) { } /** - * @param {Token} tokens - tokens + * @param {Token[]} tokens - tokens * @param {number} i - index of close token * @return {Token|null} - open token */ @@ -46,11 +50,12 @@ function findOpenToken(tokens, i) { } /** - * @param {Array} arr + * @template T + * @param {T[]} arr * @param {number} startIndex - * @param {function(any, number): boolean} test - * @param {function(any, number): boolean} [constraint] - * @return {any|undefined} element; + * @param {(elm: T) => boolean} test + * @param {(elm: T) => boolean} [constraint] + * @return {T|undefined} element; */ function find(arr, startIndex, test, constraint=()=>true) { for (let i=startIndex, len=arr.length; itrue) { if (!constraint(elm)) return; if (test(elm)) return elm; } - return; } /** - * @param {Array} arr + * @template T + * @param {T[]} arr * @param {number} startIndex - * @param {function(any, number): boolean} test - * @param {function(any, number): boolean} [constraint] - * @return {any|undefined} element; + * @param {(elm: T) => boolean} test + * @param {(elm: T) => boolean} [constraint] + * @return {T|undefined} element; */ function rfind(arr, startIndex, test, constraint=()=>true) { for (let i=startIndex; i>=0; i--) { @@ -74,14 +79,14 @@ function rfind(arr, startIndex, test, constraint=()=>true) { if (!constraint(elm)) return; if (test(elm)) return elm; } - return; } /** - * @param {Array} arr + * @template T + * @param {T[]} arr * @param {number} startIndex - * @param {function(any, number): boolean} test - * @param {function(any, number): boolean} [constraint] + * @param {(elm: T) => boolean} test + * @param {(elm: T) => boolean} [constraint] * @return {number} index ?? -1; */ function rfindIndex(arr, startIndex, test, constraint=()=>true) { diff --git a/package.json b/package.json index 7919471..4d71442 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sup39/markdown-it-attr", - "version": "1.2.0", + "version": "1.2.1", "description": "A markdown-it plugin to write id, classes, and attributes", "keywords": [ "markdown", @@ -19,9 +19,9 @@ "lib/" ], "devDependencies": { + "@sup39/eslint-config-basic": "^0.1.0", "@sup39/markdown-it-raw-table": "^1.0.0", "eslint": "^7.17.0", - "eslint-config-google": "^0.14.0", "markdown-it": "^12.0.4", "mocha": "^8.2.1" } diff --git a/yarn.lock b/yarn.lock index 03785d7..ce6d8d5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -38,6 +38,11 @@ minimatch "^3.0.4" strip-json-comments "^3.1.1" +"@sup39/eslint-config-basic@^0.1.0": + version "0.1.0" + resolved "https://registry.yarnpkg.com/@sup39/eslint-config-basic/-/eslint-config-basic-0.1.0.tgz#4b445515ddf8e70766a7b790db1314c621829b39" + integrity sha512-+YJBNJ59T3WMAG5+M6zZJj341SfyCOlq3FuvUsZWy72ESKUCcMpl1LFUuy86K7iPeGGUs8Kex4f4EQrex1DBGg== + "@sup39/markdown-it-raw-table@^1.0.0": version "1.0.0" resolved "https://registry.yarnpkg.com/@sup39/markdown-it-raw-table/-/markdown-it-raw-table-1.0.0.tgz#a8de8f03df60cba9430cc1c3f011e0b0eb05bcf6" @@ -312,11 +317,6 @@ escape-string-regexp@^1.0.5: resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4" integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ= -eslint-config-google@^0.14.0: - version "0.14.0" - resolved "https://registry.yarnpkg.com/eslint-config-google/-/eslint-config-google-0.14.0.tgz#4f5f8759ba6e11b424294a219dbfa18c508bcc1a" - integrity sha512-WsbX4WbjuMvTdeVL6+J3rK1RGhCTqjsFjX7UMSMgZiyxxaNLkoJENbrGExzERFeoTpGw3F3FypTiWAP9ZXzkEw== - eslint-scope@^5.1.1: version "5.1.1" resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-5.1.1.tgz#e786e59a66cb92b3f6c1fb0d508aab174848f48c"