1
0
Fork 0

[v1.2.1] fix @types and utils/*find function

This commit is contained in:
sup39 2021-10-16 00:55:20 +08:00
parent f86143ff62
commit 21b2b98bfb
6 changed files with 46 additions and 33 deletions

View file

@ -3,15 +3,11 @@ module.exports = {
es6: true, es6: true,
node: true, node: true,
}, },
extends: [
'google',
],
parserOptions: { parserOptions: {
ecmaVersion: 2018, ecmaVersion: 2018,
sourceType: 'module', sourceType: 'module',
}, },
rules: { extends: [
'arrow-parens': ['error', 'as-needed'], '@sup39/basic',
'indent': ['error', 2, {'MemberExpression': 1}], ],
},
}; };

View file

@ -2,8 +2,10 @@ const {parseAttrs, attrConcat} = require('./utils.js');
const {inlineAttrsApplyRules} = require('./rules.js'); const {inlineAttrsApplyRules} = require('./rules.js');
/** /**
* @param {MarkdownIt} md * @type {import('markdown-it').PluginWithOptions<{
* @param {{deliminator: string, re: RegExp}} opts * deliminator?: string,
* re?: RegExp,
* }>}
*/ */
function pluginAttr(md, opts={}) { function pluginAttr(md, opts={}) {
const deliminator = opts.deliminator || '{'; const deliminator = opts.deliminator || '{';

View file

@ -1,7 +1,17 @@
/**
* @typedef {import('markdown-it/lib/token')} Token
*/
const { const {
findOpenToken, find, rfind, rfindIndex, attrConcat, findOpenToken, find, rfind, rfindIndex, attrConcat,
} = require('./utils.js'); } = require('./utils.js');
/**
* @type {{
* name: string
* disabled?: boolean
* handler(token: Token, i: number, tokens: Token[], iBlock: number, blockTokens: Token[]): void
* }[]}
*/
const inlineAttrsApplyRules = [ const inlineAttrsApplyRules = [
{ {
name: 'first_child', name: 'first_child',
@ -22,9 +32,9 @@ const inlineAttrsApplyRules = [
}, },
{ {
name: 'after_inline_block', name: 'after_inline_block',
handler(token, i, tokens, iBlock, blockTokens) { handler(token, i, tokens) {
// find non-empty text token // 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]; const tokenC = iC>=0 && tokens[iC];
if (!tokenC) return; if (!tokenC) return;
// check tokenC type // check tokenC type

View file

@ -1,8 +1,12 @@
/**
* @typedef {import('markdown-it/lib/token')} Token
*/
/** /**
* @param {string} s - input string * @param {string} s - input string
* @param {RegExp} re - regular expression for attributes * @param {RegExp} re - regular expression for attributes
* @param {number} [limit=65536] - limit of attribute count * @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) { function parseAttrs(s, re, limit=65536) {
let count = 0; 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 * @param {number} i - index of close token
* @return {Token|null} - open 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 {number} startIndex
* @param {function(any, number): boolean} test * @param {(elm: T) => boolean} test
* @param {function(any, number): boolean} [constraint] * @param {(elm: T) => boolean} [constraint]
* @return {any|undefined} element; * @return {T|undefined} element;
*/ */
function find(arr, startIndex, test, constraint=()=>true) { function find(arr, startIndex, test, constraint=()=>true) {
for (let i=startIndex, len=arr.length; i<len; i++) { for (let i=startIndex, len=arr.length; i<len; i++) {
@ -58,15 +63,15 @@ function find(arr, startIndex, test, constraint=()=>true) {
if (!constraint(elm)) return; if (!constraint(elm)) return;
if (test(elm)) return elm; if (test(elm)) return elm;
} }
return;
} }
/** /**
* @param {Array} arr * @template T
* @param {T[]} arr
* @param {number} startIndex * @param {number} startIndex
* @param {function(any, number): boolean} test * @param {(elm: T) => boolean} test
* @param {function(any, number): boolean} [constraint] * @param {(elm: T) => boolean} [constraint]
* @return {any|undefined} element; * @return {T|undefined} element;
*/ */
function rfind(arr, startIndex, test, constraint=()=>true) { function rfind(arr, startIndex, test, constraint=()=>true) {
for (let i=startIndex; i>=0; i--) { for (let i=startIndex; i>=0; i--) {
@ -74,14 +79,14 @@ function rfind(arr, startIndex, test, constraint=()=>true) {
if (!constraint(elm)) return; if (!constraint(elm)) return;
if (test(elm)) return elm; if (test(elm)) return elm;
} }
return;
} }
/** /**
* @param {Array} arr * @template T
* @param {T[]} arr
* @param {number} startIndex * @param {number} startIndex
* @param {function(any, number): boolean} test * @param {(elm: T) => boolean} test
* @param {function(any, number): boolean} [constraint] * @param {(elm: T) => boolean} [constraint]
* @return {number} index ?? -1; * @return {number} index ?? -1;
*/ */
function rfindIndex(arr, startIndex, test, constraint=()=>true) { function rfindIndex(arr, startIndex, test, constraint=()=>true) {

View file

@ -1,6 +1,6 @@
{ {
"name": "@sup39/markdown-it-attr", "name": "@sup39/markdown-it-attr",
"version": "1.2.0", "version": "1.2.1",
"description": "A markdown-it plugin to write id, classes, and attributes", "description": "A markdown-it plugin to write id, classes, and attributes",
"keywords": [ "keywords": [
"markdown", "markdown",
@ -19,9 +19,9 @@
"lib/" "lib/"
], ],
"devDependencies": { "devDependencies": {
"@sup39/eslint-config-basic": "^0.1.0",
"@sup39/markdown-it-raw-table": "^1.0.0", "@sup39/markdown-it-raw-table": "^1.0.0",
"eslint": "^7.17.0", "eslint": "^7.17.0",
"eslint-config-google": "^0.14.0",
"markdown-it": "^12.0.4", "markdown-it": "^12.0.4",
"mocha": "^8.2.1" "mocha": "^8.2.1"
} }

View file

@ -38,6 +38,11 @@
minimatch "^3.0.4" minimatch "^3.0.4"
strip-json-comments "^3.1.1" 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": "@sup39/markdown-it-raw-table@^1.0.0":
version "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" 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" resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ= 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: eslint-scope@^5.1.1:
version "5.1.1" version "5.1.1"
resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-5.1.1.tgz#e786e59a66cb92b3f6c1fb0d508aab174848f48c" resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-5.1.1.tgz#e786e59a66cb92b3f6c1fb0d508aab174848f48c"