[v1.2.1] fix @types and utils/*find function
This commit is contained in:
parent
f86143ff62
commit
21b2b98bfb
6 changed files with 46 additions and 33 deletions
10
.eslintrc.js
10
.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',
|
||||
],
|
||||
};
|
||||
|
|
|
@ -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 || '{';
|
||||
|
|
14
lib/rules.js
14
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
|
||||
|
|
35
lib/utils.js
35
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; i<len; i++) {
|
||||
|
@ -58,15 +63,15 @@ function find(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]
|
||||
* @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) {
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
|
|
10
yarn.lock
10
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"
|
||||
|
|
Reference in a new issue