This commit is contained in:
inherentlee committed 2026-03-31 16:38:22 -07:00
commit 38940436a7
2112 files changed
+376929

No files matched your search

+22
View File
@@ -0,0 +1,22 @@
The MIT License (MIT)
Copyright (c) Arve Seljebu <arve.seljebu@gmail.com> (arve0.github.io)
Copyright (C) 2022 - PRESENT by MrHope
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
+18
View File
@@ -0,0 +1,18 @@
# @mdit/plugin-attrs
[![Version](https://img.shields.io/npm/v/@mdit/plugin-attrs.svg?style=flat-square&logo=npm) ![Downloads](https://img.shields.io/npm/dm/@mdit/plugin-attrs.svg?style=flat-square&logo=npm) ![Size](https://img.shields.io/bundlephobia/min/@mdit/plugin-attrs?style=flat-square&logo=npm)](https://www.npmjs.com/package/@mdit/plugin-attrs)
Attrs plugin for MarkdownIt.
## [Docs](https://mdit-plugins.github.io/attrs.html) | [文档](https://mdit-plugins.github.io/zh/attrs.html)
## Install / 安装
```bash
# pnpm
pnpm add -D @mdit/plugin-attrs
# npm
npm i -D @mdit/plugin-attrs
# yarn
yarn add -D @mdit/plugin-attrs
```
+3
View File
@@ -0,0 +1,3 @@
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.mdItPluginAttrs={}))})(this,function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});let t=e=>!(e===9||e===10||e===12||e===32||e===47||e===62||e===34||e===39||e===61),n=(e,n,r)=>{let i=``,a=``,o=!0,s=!1,c=[];for(let r=n[0];r<n[1];r++){let n=e.charCodeAt(r);if(n===61&&o){o=!1;continue}if(n===46&&i===``){e.charCodeAt(r+1)===46?(i=`css-module`,r++):i=`class`,o=!1;continue}if(n===35&&i===``){i=`id`,o=!1;continue}if(n===34&&a===``&&!s){s=!0;continue}if(n===34&&s){s=!1;continue}if(n===32&&!s){if(i===``)continue;c.push([i,a]),i=``,a=``,o=!0;continue}if(!(o&&!t(n))){if(o){i+=String.fromCharCode(n);continue}a+=String.fromCharCode(n)}}return i!==``&&c.push([i,a]),r.length>0?c.filter(([e])=>r.some(t=>t instanceof RegExp?t.test(e):t===e)):c},r=(e,t,r,i)=>{e&&n(t,r,i).forEach(([t,n])=>{switch(t){case`class`:e.attrJoin(`class`,n);break;case`css-module`:e.attrJoin(`css-module`,n);break;default:e.attrPush([t,n])}})},i=(e,t)=>{if(![`start`,`end`,`only`].includes(t))throw Error(`Invalid 'where' parameter: ${t}. Expected 'start', 'end', or 'only'.`);let n=e.left,r=e.right,i=n.length,a=r.length,o=i+1+a;return e=>{if(typeof e!=`string`||e.length<o)return!1;let s,c;if(t===`start`){if(!e.startsWith(n)||(s=i,c=e.indexOf(r,i+1),c===-1))return!1;let t=c+a;if(t<e.length&&r.includes(e.charAt(t)))return!1}else if(t===`end`){if(s=e.lastIndexOf(n),s===-1||(c=e.indexOf(r,s+i+1),s+=i,c===-1||c+a!==e.length))return!1}else{if(!e.startsWith(n)||!e.endsWith(r))return!1;s=i,c=e.length-a}let l=e.charCodeAt(s),u=c-s;return(l===46||l===35?u>=2:u>=1)?[s,c]:!1}},a=(e,t)=>{let n=e[t];if(n.type===`softbreak`)return null;if(n.nesting===0)return n;let r=n.level,i=n.type.replace(`_close`,`_open`);for(;t>=0;){let n=e[t];if(n.type===i&&n.level===r)return n;t--}return null},o=(e,t)=>t>=0?e[t]:e[e.length+t],s=(e,t,n)=>{let r={match:!1,position:null,range:null},i=typeof n.shift==`number`,a=i?t+n.shift:n.position;if(i&&a<0)return r;let c=o(e,a);if(!c)return r;let l=Object.keys(n),u=l.length;for(let e=0;e<u;e++){let t=l[e];if(t===`shift`||t===`position`)continue;if(c[t]==null)return r;if(t===`children`&&Array.isArray(n.children)){if(!c.children?.length)return r;let e=n.children,t=c.children,i,a=null;if(e.every(e=>typeof e.position==`number`)){if(i=e.every(e=>{let n=s(t,e.position,e);return n.match?(n.range&&(a=n.range),!0):!1}),i){let{position:n}=e[e.length-1];r.position=n>=0?n:t.length+n,r.range=a}}else for(let n=0;n<t.length;n++)if(i=e.every(e=>{let r=s(t,n,e);return r.match?(r.range&&(a=r.range),!0):!1}),i){r.position=n,a&&(r.range=a);break}if(i===!1)return r;continue}let i=n[t];switch(typeof i){case`boolean`:case`number`:case`string`:if(c[t]!==i)return r;break;case`function`:{let e=i(c[t]);if(!e)return r;Array.isArray(e)&&(r.range=e);break}default:throw Error(`Unknown type of pattern test (key: ${t}). Test should be of type boolean, number, string or function.`)}}return r.match=!0,r},c=e=>e,l=(e,t)=>{let n=e.utils.isSpace;return c({name:`end of block`,tests:[{shift:0,type:`inline`,children:[{position:-1,content:i(t,`end`),type:e=>e!==`code_inline`&&e!==`math_inline`}]}],transform:(e,i,o,s)=>{let c=s[0]-t.left.length,l=e[i].children[o],{content:u}=l,d=n(u.charCodeAt(c-1)),f=i+1;for(;e[f+1]?.nesting===-1;)f++;r(a(e,f),u,s,t.allowed),l.content=u.slice(0,d?c-1:c)}})},u=(e,t)=>{let n=e.utils.isSpace;return c({name:`code-block`,tests:[{shift:0,block:!0,info:i(t,`end`)}],transform:(e,i,a,o)=>{let s=o[0]-t.left.length,c=e[i],{info:l}=c,u=n(l.charCodeAt(s-1));r(c,l,o,t.allowed),c.info=l.slice(0,u?s-1:s)}})},d=(e,t)=>{let n=e.utils.isSpace;return c({name:`end of block`,tests:[{shift:-1,type:`heading_open`},{shift:0,type:`inline`,children:[{position:-1,content:i(t,`end`),type:e=>e!==`code_inline`&&e!==`math_inline`}]}],transform:(e,i,o,s)=>{let c=s[0]-t.left.length,l=e[i].children[o],{content:u}=l,d=n(u.charCodeAt(c-1));r(a(e,i+1),u,s,t.allowed),l.content=u.slice(0,d?c-1:c)}})},f=e=>[c({name:`inline nesting self-close`,tests:[{shift:0,type:`inline`,children:[{shift:-1,type:e=>e===`image`||e===`code_inline`},{shift:0,type:`text`,content:i(e,`start`)}]}],transform:(t,n,i,a)=>{let o=t[n].children,s=o[i],c=o[i-1],l=e.right.length+a[1];r(c,s.content,a,e.allowed),s.content.length===l?o.splice(i,1):s.content=s.content.slice(l)}}),c({name:`inline attributes`,tests:[{shift:0,type:`inline`,children:[{shift:-1,nesting:-1},{shift:0,type:`text`,content:i(e,`start`)}]}],transform:(t,n,i,o)=>{let s=t[n].children,c=s[i],{content:l}=c,u=e.right.length+o[1];r(a(s,i-1),l,o,e.allowed),c.content=l.slice(u)}})],p=(e,t)=>{let n=e.utils.isSpace,o=t.allowed;return[c({name:`list softbreak`,tests:[{shift:-2,type:`list_item_open`},{shift:0,type:`inline`,children:[{position:-2,type:`softbreak`},{position:-1,type:`text`,content:i(t,`only`)}]}],transform:(e,t,n,i)=>{let a=e[t].children,s=a[n],c=t-2;for(;e[cLine truncated
{.a} softbreak then curly in start`,tests:[{shift:0,type:`inline`,children:[{position:-2,type:`softbreak`},{position:-1,type:`text`,content:i(e,`only`)}]}],transform:(t,n,i,o)=>{let s=t[n].children,c=s[i],l=n+1;for(;t[l+1]?.nesting===-1;)l++;r(a(t,l),c.content,o,e.allowed),t[n].children=s.slice(0,-2)}}),h=(e,t)=>{let n=e.utils.isSpace;return c({name:`horizontal rule`,tests:[{shift:0,type:`paragraph_open`},{shift:1,type:`inline`,children:e=>e.length===1,content:e=>{let r=0,a,o=e.charCodeAt(r++);if(o!==45&&o!==42&&o!==95)return!1;let s=1;for(;r<e.length&&(a=e.charCodeAt(r++),a===o);)s++;return s<3?!1:(n(e.charCodeAt(r-1))||r--,i(t,`end`)(e))}},{shift:2,type:`paragraph_close`}],transform:(e,n,i,a)=>{let o=e[n],{content:s}=e[n+1];o.type=`hr`,o.tag=`hr`,o.nesting=0,r(o,s,a,t.allowed),o.markup=s,e.splice(n+1,2)}})},g=(e,t)=>{let n=e.utils.isSpace,o=t.allowed;return[c({name:`table`,tests:[{shift:0,type:`table_close`},{shift:1,type:`paragraph_open`},{shift:2,type:`inline`,content:i(t,`only`)}],transform:(e,t,n,i)=>{let s=e[t+2];r(a(e,t),s.content,i,o),e.splice(t+1,3)}}),c({name:`table cell attributes`,tests:[{shift:-1,type:e=>e===`td_open`||e===`th_open`},{shift:0,type:`inline`,children:[{shift:0,type:`text`,content:i(t,`end`)}]}],transform:(e,i,a,s)=>{let c=s[0]-t.left.length,l=e[i].children[a],u=e[i-1],{content:d}=l,f=n(d.charCodeAt(c-1));r(u,d,s,o),l.content=d.slice(0,f?c-1:c)}}),c({name:`table thead metadata`,tests:[{shift:0,type:`tr_close`},{shift:1,type:`thead_close`},{shift:2,type:`tbody_open`}],transform:(e,t)=>{let n=a(e,t),r=e[t-1],i=0,o=t-1;for(;o>0;){let t=e[o];if(t===n){let t=e[o-1];t.meta={...t.meta,columnCount:i};break}t.level===r.level&&t.type===r.type&&i++,o--}let s=e[t+2];s.meta={...s.meta,columnCount:i}}}),c({name:`table tbody calculate`,tests:[{shift:0,type:`tbody_close`,hidden:!1}],transform:(e,t)=>{let n=t-2;for(;n>=0&&e[n].type!==`tbody_open`;)n--;let r=e[n].meta.columnCount;if(r<2)return;let i=Array.from({length:r}).fill(0),a=[],o=n+1;for(;o<t;){let n=o,s=n+1;for(;s<t&&e[s].type!==`tr_close`;)s++;let c=[],l=n+1;for(;l<s;){let t=l+1;for(;t<s&&e[t].type!==`td_close`&&e[t].type!==`th_close`;)t++;c.push([l,t]),l=t+1}let u=0,d=0;for(;d<r;){if(i[d]>0){i[d]--,a.push(c[u]),u++,d++;continue}let t=e[c[u][0]],n=Number(t.attrGet(`colspan`))||1,o=Number(t.attrGet(`rowspan`))||1;u++;let s=0;for(let e=0;e<n;e++)if(d+e<r)if(i[d+e]===0)s++;else break;n>1&&s<n&&t.attrSet(`colspan`,String(s));for(let e=0;e<s;e++)i[d+e]=o-1;let l=s-1;if(l>0)for(let e=0;e<l;e++)a.push(c[u]),u++;d+=s}o=s+1}a.sort((e,t)=>t[0]-e[0]);for(let[t,n]of a)e.splice(t,n-t+1)}})]},_=[`fence`,`inline`,`table`,`list`,`heading`,`hr`,`softbreak`,`block`],v=(e,t)=>{let n=t.rule===!1?[]:Array.isArray(t.rule)?t.rule.filter(e=>_.includes(e)):_,r=[];return n.includes(`fence`)&&r.push(u(e,t)),n.includes(`inline`)&&r.push(...f(t)),n.includes(`table`)&&r.push(...g(e,t)),n.includes(`list`)&&r.push(...p(e,t)),n.includes(`softbreak`)&&r.push(m(t)),n.includes(`hr`)&&r.push(h(e,t)),n.includes(`block`)?r.push(l(e,t)):n.includes(`heading`)&&r.push(d(e,t)),r};e.attrs=(e,{left:t=`{`,right:n=`}`,allowed:r=[],rule:i=`all`}={})=>{let a=v(e,{left:t,right:n,allowed:r,rule:i});e.core.ruler.before(`linkify`,`attrs`,e=>{let t=e.tokens;for(let e=0;e<t.length;e++)for(let n=0;n<a.length;n++){let r=a[n],i=null,o=null;r.tests.every(n=>{let r=s(t,e,n);return r.position!=null&&({position:i}=r),r.range&&(o=r.range),r.match})&&(r.transform(t,e,i,o),(r.name===`inline attributes`||r.name===`inline nesting self-close`)&&n--)}})}});
//# sourceMappingURL=cdn.umd.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"cdn.umd.js","names":[],"sources":["../src/helper/constants.ts","../src/helper/getAttrs.ts","../src/helper/addAttrs.ts","../src/helper/getDelimiterChecker.ts","../src/helper/getMatchingOpeningToken.ts","../src/utils.ts","../src/helper/testRule.ts","../src/rules/types.ts","../src/rules/block.ts","../src/rules/fence.ts","../src/rules/heading.ts","../src/rules/inline.ts","../src/rules/list.ts","../src/rules/softbreak.ts","../src/rules/hr.ts","../src/rules/table.ts","../src/rules/rules.ts","../src/plugin.ts"],"sourcesContent":["/** . */\nexport const CLASS_MARKER = 46;\n/** # */\nexport const ID_MARKER = 35;\n/** space */\nexport const PAIR_SEPARATOR = 32;\n/** = */\nexport const KEY_SEPARATOR = 61;\n/** \" */\nexport const QUOTE_MARKER = 34;\n","import {\n CLASS_MARKER,\n ID_MARKER,\n KEY_SEPARATOR,\n PAIR_SEPARATOR,\n QUOTE_MARKER,\n} from \"./constants.js\";\nimport type { Attr } from \"./types.js\";\nimport type { DelimiterRange } from \"../rules/types.js\";\n\nconst isAllowedKeyChar = (charCode: number): boolean =>\n !(\n (\n charCode === 9 /* \\t */ ||\n charCode === 10 /* \\n */ ||\n charCode === 12 /* \\f */ ||\n charCode === 32 /* 空格 */ ||\n charCode === 47 /* / */ ||\n charCode === 62 /* > */ ||\n charCode === 34 /* \" */ ||\n charCode === 39 /* ' */ ||\n charCode === 61\n ) /* = */\n );\n\nexport const getAttrs = (\n str: string,\n range: DelimiterRange,\n allowed: (string | RegExp)[],\n): Attr[] => {\n let key = \"\";\n let value = \"\";\n let parsingKey = true;\n let valueInsideQuotes = false;\n\n const attrs: Attr[] = [];\n\n // read inside marker\n // start + left delimiter length to avoid beginning marker\n // breaks when ending marker is found or end of string\n for (let index = range[0]; index < range[1]; index++) {\n const charCode = str.charCodeAt(index);\n\n // switch to reading value if equal sign\n if (charCode === KEY_SEPARATOR && parsingKey) {\n parsingKey = false;\n continue;\n }\n\n // {.class} {..css-module}\n if (charCode === CLASS_MARKER && key === \"\") {\n if (str.charCodeAt(index + 1) === CLASS_MARKER) {\n key = \"css-module\";\n index++;\n } else {\n key = \"class\";\n }\n\n parsingKey = false;\n continue;\n }\n\n // {#id}\n if (charCode === ID_MARKER && key === \"\") {\n key = \"id\";\n parsingKey = false;\n continue;\n }\n\n // {value=\"inside quotes\"}\n if (charCode === QUOTE_MARKER && value === \"\" && !valueInsideQuotes) {\n valueInsideQuotes = true;\n continue;\n }\n\n if (charCode === QUOTE_MARKER && valueInsideQuotes) {\n valueInsideQuotes = false;\n continue;\n }\n\n // read next key/value pair\n if (charCode === PAIR_SEPARATOR && !valueInsideQuotes) {\n if (key === \"\") {\n // beginning or ending space: { .red } vs {.red}\n continue;\n }\n\n attrs.push([key, value]);\n key = \"\";\n value = \"\";\n parsingKey = true;\n\n continue;\n }\n\n // continue if character not allowed\n if (parsingKey && !isAllowedKeyChar(charCode)) continue;\n\n // no other conditions met; append to key/value\n if (parsingKey) {\n key += String.fromCharCode(charCode);\n continue;\n }\n\n value += String.fromCharCode(charCode);\n }\n\n // append last key/value pair\n if (key !== \"\") attrs.push([key, value]);\n\n return allowed.length > 0\n ? attrs.filter(([attr]) =>\n allowed.some((item) => (item instanceof RegExp ? item.test(attr) : item === attr)),\n )\n : attrs;\n};\n","import type Token from \"markdown-it/lib/token.mjs\";\n\nimport { getAttrs } from \"./getAttrs.js\";\nimport type { DelimiterRange } from \"../rules/types.js\";\n\nexport const addAttrs = (\n token: Token | null,\n content: string,\n range: DelimiterRange,\n allowed: (string | RegExp)[],\n): void => {\n if (!token) return;\n\n getAttrs(content, range, allowed).forEach(([key, value]) => {\n switch (key) {\n case \"class\": {\n token.attrJoin(\"class\", value);\n break;\n }\n case \"css-module\": {\n token.attrJoin(\"css-module\", value);\n break;\n }\n default: {\n token.attrPush([key, value]);\n }\n }\n });\n};\n","import { CLASS_MARKER, ID_MARKER } from \"./constants.js\";\nimport type { DelimiterConfig } from \"./types.js\";\nimport type { DelimiterChecker } from \"../rules/types.js\";\n\n/**\n * Get a function to check if a string matches the delimiter pattern\n * 获取一个函数来检查字符串是否匹配分隔符模式\n *\n * @param options - Delimiter configuration / 分隔符配置\n * @param where - Position for matching: start, end, or only / 匹配位置:开始、结束或仅匹配\n * @returns A function that checks if content matches the delimiter pattern / 检查内容是否匹配分隔符Line truncated
+52
View File
@@ -0,0 +1,52 @@
import { PluginWithOptions } from "markdown-it";
//#region src/helper/types.d.ts
interface DelimiterConfig {
/**
* left delimiter
*
* 左分隔符
*
* @default '{'
*/
left: string;
/**
* right delimiter
*
* 右分隔符
*
* @default '}'
*/
right: string;
/**
* allowed attributes
*
* @description An empty list means allowing all attribute
*
* 允许的属性
*
* @description 设置空数组意味着允许所有属性
*
* @default []
*/
allowed: (string | RegExp)[];
}
//#endregion
//#region src/options.d.ts
type MarkdownItAttrRuleName = "fence" | "inline" | "table" | "list" | "heading" | "hr" | "softbreak" | "block";
interface MarkdownItAttrsOptions extends Partial<DelimiterConfig> {
/**
* Rules to enable
*
* 启用的规则
*
* @default "all"
*/
rule?: "all" | boolean | MarkdownItAttrRuleName[];
}
//#endregion
//#region src/plugin.d.ts
declare const attrs: PluginWithOptions<MarkdownItAttrsOptions>;
//#endregion
export { MarkdownItAttrRuleName, MarkdownItAttrsOptions, attrs };
//# sourceMappingURL=index.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/helper/types.ts","../src/options.ts","../src/plugin.ts"],"mappings":";;;UAEiB,eAAA;;AAAjB;;;;;;EAQE,IAAA;;;;;;;ACRF;EDiBE,KAAA;;;;ACPF;;;;;;;;EDoBE,OAAA,YAAmB,MAAA;AAAA;;;KC9BT,sBAAA;AAAA,UAUK,sBAAA,SAA+B,OAAA,CAAQ,eAAA;EDVxD;;;;;;;ECkBE,IAAA,qBAAyB,sBAAA;AAAA;;;cCRd,KAAA,EAAO,iBAAA,CAAkB,sBAAA"}
+3
View File
@@ -0,0 +1,3 @@
const e=e=>!(e===9||e===10||e===12||e===32||e===47||e===62||e===34||e===39||e===61),t=(t,n,r)=>{let i=``,a=``,o=!0,s=!1,c=[];for(let r=n[0];r<n[1];r++){let n=t.charCodeAt(r);if(n===61&&o){o=!1;continue}if(n===46&&i===``){t.charCodeAt(r+1)===46?(i=`css-module`,r++):i=`class`,o=!1;continue}if(n===35&&i===``){i=`id`,o=!1;continue}if(n===34&&a===``&&!s){s=!0;continue}if(n===34&&s){s=!1;continue}if(n===32&&!s){if(i===``)continue;c.push([i,a]),i=``,a=``,o=!0;continue}if(!(o&&!e(n))){if(o){i+=String.fromCharCode(n);continue}a+=String.fromCharCode(n)}}return i!==``&&c.push([i,a]),r.length>0?c.filter(([e])=>r.some(t=>t instanceof RegExp?t.test(e):t===e)):c},n=(e,n,r,i)=>{e&&t(n,r,i).forEach(([t,n])=>{switch(t){case`class`:e.attrJoin(`class`,n);break;case`css-module`:e.attrJoin(`css-module`,n);break;default:e.attrPush([t,n])}})},r=(e,t)=>{if(![`start`,`end`,`only`].includes(t))throw Error(`Invalid 'where' parameter: ${t}. Expected 'start', 'end', or 'only'.`);let n=e.left,r=e.right,i=n.length,a=r.length,o=i+1+a;return e=>{if(typeof e!=`string`||e.length<o)return!1;let s,c;if(t===`start`){if(!e.startsWith(n)||(s=i,c=e.indexOf(r,i+1),c===-1))return!1;let t=c+a;if(t<e.length&&r.includes(e.charAt(t)))return!1}else if(t===`end`){if(s=e.lastIndexOf(n),s===-1||(c=e.indexOf(r,s+i+1),s+=i,c===-1||c+a!==e.length))return!1}else{if(!e.startsWith(n)||!e.endsWith(r))return!1;s=i,c=e.length-a}let l=e.charCodeAt(s),u=c-s;return(l===46||l===35?u>=2:u>=1)?[s,c]:!1}},i=(e,t)=>{let n=e[t];if(n.type===`softbreak`)return null;if(n.nesting===0)return n;let r=n.level,i=n.type.replace(`_close`,`_open`);for(;t>=0;){let n=e[t];if(n.type===i&&n.level===r)return n;t--}return null},a=(e,t)=>t>=0?e[t]:e[e.length+t],o=(e,t,n)=>{let r={match:!1,position:null,range:null},i=typeof n.shift==`number`,s=i?t+n.shift:n.position;if(i&&s<0)return r;let c=a(e,s);if(!c)return r;let l=Object.keys(n),u=l.length;for(let e=0;e<u;e++){let t=l[e];if(t===`shift`||t===`position`)continue;if(c[t]==null)return r;if(t===`children`&&Array.isArray(n.children)){if(!c.children?.length)return r;let e=n.children,t=c.children,i,a=null;if(e.every(e=>typeof e.position==`number`)){if(i=e.every(e=>{let n=o(t,e.position,e);return n.match?(n.range&&(a=n.range),!0):!1}),i){let{position:n}=e[e.length-1];r.position=n>=0?n:t.length+n,r.range=a}}else for(let n=0;n<t.length;n++)if(i=e.every(e=>{let r=o(t,n,e);return r.match?(r.range&&(a=r.range),!0):!1}),i){r.position=n,a&&(r.range=a);break}if(i===!1)return r;continue}let i=n[t];switch(typeof i){case`boolean`:case`number`:case`string`:if(c[t]!==i)return r;break;case`function`:{let e=i(c[t]);if(!e)return r;Array.isArray(e)&&(r.range=e);break}default:throw Error(`Unknown type of pattern test (key: ${t}). Test should be of type boolean, number, string or function.`)}}return r.match=!0,r},s=e=>e,c=(e,t)=>{let a=e.utils.isSpace;return s({name:`end of block`,tests:[{shift:0,type:`inline`,children:[{position:-1,content:r(t,`end`),type:e=>e!==`code_inline`&&e!==`math_inline`}]}],transform:(e,r,o,s)=>{let c=s[0]-t.left.length,l=e[r].children[o],{content:u}=l,d=a(u.charCodeAt(c-1)),f=r+1;for(;e[f+1]?.nesting===-1;)f++;n(i(e,f),u,s,t.allowed),l.content=u.slice(0,d?c-1:c)}})},l=(e,t)=>{let i=e.utils.isSpace;return s({name:`code-block`,tests:[{shift:0,block:!0,info:r(t,`end`)}],transform:(e,r,a,o)=>{let s=o[0]-t.left.length,c=e[r],{info:l}=c,u=i(l.charCodeAt(s-1));n(c,l,o,t.allowed),c.info=l.slice(0,u?s-1:s)}})},u=(e,t)=>{let a=e.utils.isSpace;return s({name:`end of block`,tests:[{shift:-1,type:`heading_open`},{shift:0,type:`inline`,children:[{position:-1,content:r(t,`end`),type:e=>e!==`code_inline`&&e!==`math_inline`}]}],transform:(e,r,o,s)=>{let c=s[0]-t.left.length,l=e[r].children[o],{content:u}=l,d=a(u.charCodeAt(c-1));n(i(e,r+1),u,s,t.allowed),l.content=u.slice(0,d?c-1:c)}})},d=e=>[s({name:`inline nesting self-close`,tests:[{shift:0,type:`inline`,children:[{shift:-1,type:e=>e===`image`||e===`code_inline`},{shift:0,type:`text`,content:r(e,`start`)}]}],transform:(t,r,i,a)=>{let o=t[r].children,s=o[i],c=o[i-1],l=e.right.length+a[1];n(c,s.content,a,e.allowed),s.content.length===l?o.splice(i,1):s.content=s.content.slice(l)}}),s({name:`inline attributes`,tests:[{shift:0,type:`inline`,children:[{shift:-1,nesting:-1},{shift:0,type:`text`,content:r(e,`start`)}]}],transform:(t,r,a,o)=>{let s=t[r].children,c=s[a],{content:l}=c,u=e.right.length+o[1];n(i(s,a-1),l,o,e.allowed),c.content=l.slice(u)}})],f=(e,t)=>{let a=e.utils.isSpace,o=t.allowed;return[s({name:`list softbreak`,tests:[{shift:-2,type:`list_item_open`},{shift:0,type:`inline`,children:[{position:-2,type:`softbreak`},{position:-1,type:`text`,content:r(t,`only`)}]}],transform:(e,t,r,i)=>{let a=e[t].children,s=a[r],c=t-2;for(;e[c-1]?.type!==`ordered_list_open`&&e[c-1].type!==`bullet_list_open`;)c--;n(e[c-1],s.content,i,o),e[t].children=a.slice(0,-2)}}),s({name:`list double softbreak`,tests:[{shift:0,type:e=>e===`bullet_list_close`||e===`ordered_list_close`},{shift:1,type:`paragraph_open`},{shift:2,typLine truncated
{.a} softbreak then curly in start`,tests:[{shift:0,type:`inline`,children:[{position:-2,type:`softbreak`},{position:-1,type:`text`,content:r(e,`only`)}]}],transform:(t,r,a,o)=>{let s=t[r].children,c=s[a],l=r+1;for(;t[l+1]?.nesting===-1;)l++;n(i(t,l),c.content,o,e.allowed),t[r].children=s.slice(0,-2)}}),m=(e,t)=>{let i=e.utils.isSpace;return s({name:`horizontal rule`,tests:[{shift:0,type:`paragraph_open`},{shift:1,type:`inline`,children:e=>e.length===1,content:e=>{let n=0,a,o=e.charCodeAt(n++);if(o!==45&&o!==42&&o!==95)return!1;let s=1;for(;n<e.length&&(a=e.charCodeAt(n++),a===o);)s++;return s<3?!1:(i(e.charCodeAt(n-1))||n--,r(t,`end`)(e))}},{shift:2,type:`paragraph_close`}],transform:(e,r,i,a)=>{let o=e[r],{content:s}=e[r+1];o.type=`hr`,o.tag=`hr`,o.nesting=0,n(o,s,a,t.allowed),o.markup=s,e.splice(r+1,2)}})},h=(e,t)=>{let a=e.utils.isSpace,o=t.allowed;return[s({name:`table`,tests:[{shift:0,type:`table_close`},{shift:1,type:`paragraph_open`},{shift:2,type:`inline`,content:r(t,`only`)}],transform:(e,t,r,a)=>{let s=e[t+2];n(i(e,t),s.content,a,o),e.splice(t+1,3)}}),s({name:`table cell attributes`,tests:[{shift:-1,type:e=>e===`td_open`||e===`th_open`},{shift:0,type:`inline`,children:[{shift:0,type:`text`,content:r(t,`end`)}]}],transform:(e,r,i,s)=>{let c=s[0]-t.left.length,l=e[r].children[i],u=e[r-1],{content:d}=l,f=a(d.charCodeAt(c-1));n(u,d,s,o),l.content=d.slice(0,f?c-1:c)}}),s({name:`table thead metadata`,tests:[{shift:0,type:`tr_close`},{shift:1,type:`thead_close`},{shift:2,type:`tbody_open`}],transform:(e,t)=>{let n=i(e,t),r=e[t-1],a=0,o=t-1;for(;o>0;){let t=e[o];if(t===n){let t=e[o-1];t.meta={...t.meta,columnCount:a};break}t.level===r.level&&t.type===r.type&&a++,o--}let s=e[t+2];s.meta={...s.meta,columnCount:a}}}),s({name:`table tbody calculate`,tests:[{shift:0,type:`tbody_close`,hidden:!1}],transform:(e,t)=>{let n=t-2;for(;n>=0&&e[n].type!==`tbody_open`;)n--;let r=e[n].meta.columnCount;if(r<2)return;let i=Array.from({length:r}).fill(0),a=[],o=n+1;for(;o<t;){let n=o,s=n+1;for(;s<t&&e[s].type!==`tr_close`;)s++;let c=[],l=n+1;for(;l<s;){let t=l+1;for(;t<s&&e[t].type!==`td_close`&&e[t].type!==`th_close`;)t++;c.push([l,t]),l=t+1}let u=0,d=0;for(;d<r;){if(i[d]>0){i[d]--,a.push(c[u]),u++,d++;continue}let t=e[c[u][0]],n=Number(t.attrGet(`colspan`))||1,o=Number(t.attrGet(`rowspan`))||1;u++;let s=0;for(let e=0;e<n;e++)if(d+e<r)if(i[d+e]===0)s++;else break;n>1&&s<n&&t.attrSet(`colspan`,String(s));for(let e=0;e<s;e++)i[d+e]=o-1;let l=s-1;if(l>0)for(let e=0;e<l;e++)a.push(c[u]),u++;d+=s}o=s+1}a.sort((e,t)=>t[0]-e[0]);for(let[t,n]of a)e.splice(t,n-t+1)}})]},g=[`fence`,`inline`,`table`,`list`,`heading`,`hr`,`softbreak`,`block`],_=(e,t)=>{let n=t.rule===!1?[]:Array.isArray(t.rule)?t.rule.filter(e=>g.includes(e)):g,r=[];return n.includes(`fence`)&&r.push(l(e,t)),n.includes(`inline`)&&r.push(...d(t)),n.includes(`table`)&&r.push(...h(e,t)),n.includes(`list`)&&r.push(...f(e,t)),n.includes(`softbreak`)&&r.push(p(t)),n.includes(`hr`)&&r.push(m(e,t)),n.includes(`block`)?r.push(c(e,t)):n.includes(`heading`)&&r.push(u(e,t)),r},v=(e,{left:t=`{`,right:n=`}`,allowed:r=[],rule:i=`all`}={})=>{let a=_(e,{left:t,right:n,allowed:r,rule:i});e.core.ruler.before(`linkify`,`attrs`,e=>{let t=e.tokens;for(let e=0;e<t.length;e++)for(let n=0;n<a.length;n++){let r=a[n],i=null,s=null;r.tests.every(n=>{let r=o(t,e,n);return r.position!=null&&({position:i}=r),r.range&&(s=r.range),r.match})&&(r.transform(t,e,i,s),(r.name===`inline attributes`||r.name===`inline nesting self-close`)&&n--)}})};export{v as attrs};
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.js","names":[],"sources":["../src/helper/constants.ts","../src/helper/getAttrs.ts","../src/helper/addAttrs.ts","../src/helper/getDelimiterChecker.ts","../src/helper/getMatchingOpeningToken.ts","../src/utils.ts","../src/helper/testRule.ts","../src/rules/types.ts","../src/rules/block.ts","../src/rules/fence.ts","../src/rules/heading.ts","../src/rules/inline.ts","../src/rules/list.ts","../src/rules/softbreak.ts","../src/rules/hr.ts","../src/rules/table.ts","../src/rules/rules.ts","../src/plugin.ts"],"sourcesContent":["/** . */\nexport const CLASS_MARKER = 46;\n/** # */\nexport const ID_MARKER = 35;\n/** space */\nexport const PAIR_SEPARATOR = 32;\n/** = */\nexport const KEY_SEPARATOR = 61;\n/** \" */\nexport const QUOTE_MARKER = 34;\n","import {\n CLASS_MARKER,\n ID_MARKER,\n KEY_SEPARATOR,\n PAIR_SEPARATOR,\n QUOTE_MARKER,\n} from \"./constants.js\";\nimport type { Attr } from \"./types.js\";\nimport type { DelimiterRange } from \"../rules/types.js\";\n\nconst isAllowedKeyChar = (charCode: number): boolean =>\n !(\n (\n charCode === 9 /* \\t */ ||\n charCode === 10 /* \\n */ ||\n charCode === 12 /* \\f */ ||\n charCode === 32 /* 空格 */ ||\n charCode === 47 /* / */ ||\n charCode === 62 /* > */ ||\n charCode === 34 /* \" */ ||\n charCode === 39 /* ' */ ||\n charCode === 61\n ) /* = */\n );\n\nexport const getAttrs = (\n str: string,\n range: DelimiterRange,\n allowed: (string | RegExp)[],\n): Attr[] => {\n let key = \"\";\n let value = \"\";\n let parsingKey = true;\n let valueInsideQuotes = false;\n\n const attrs: Attr[] = [];\n\n // read inside marker\n // start + left delimiter length to avoid beginning marker\n // breaks when ending marker is found or end of string\n for (let index = range[0]; index < range[1]; index++) {\n const charCode = str.charCodeAt(index);\n\n // switch to reading value if equal sign\n if (charCode === KEY_SEPARATOR && parsingKey) {\n parsingKey = false;\n continue;\n }\n\n // {.class} {..css-module}\n if (charCode === CLASS_MARKER && key === \"\") {\n if (str.charCodeAt(index + 1) === CLASS_MARKER) {\n key = \"css-module\";\n index++;\n } else {\n key = \"class\";\n }\n\n parsingKey = false;\n continue;\n }\n\n // {#id}\n if (charCode === ID_MARKER && key === \"\") {\n key = \"id\";\n parsingKey = false;\n continue;\n }\n\n // {value=\"inside quotes\"}\n if (charCode === QUOTE_MARKER && value === \"\" && !valueInsideQuotes) {\n valueInsideQuotes = true;\n continue;\n }\n\n if (charCode === QUOTE_MARKER && valueInsideQuotes) {\n valueInsideQuotes = false;\n continue;\n }\n\n // read next key/value pair\n if (charCode === PAIR_SEPARATOR && !valueInsideQuotes) {\n if (key === \"\") {\n // beginning or ending space: { .red } vs {.red}\n continue;\n }\n\n attrs.push([key, value]);\n key = \"\";\n value = \"\";\n parsingKey = true;\n\n continue;\n }\n\n // continue if character not allowed\n if (parsingKey && !isAllowedKeyChar(charCode)) continue;\n\n // no other conditions met; append to key/value\n if (parsingKey) {\n key += String.fromCharCode(charCode);\n continue;\n }\n\n value += String.fromCharCode(charCode);\n }\n\n // append last key/value pair\n if (key !== \"\") attrs.push([key, value]);\n\n return allowed.length > 0\n ? attrs.filter(([attr]) =>\n allowed.some((item) => (item instanceof RegExp ? item.test(attr) : item === attr)),\n )\n : attrs;\n};\n","import type Token from \"markdown-it/lib/token.mjs\";\n\nimport { getAttrs } from \"./getAttrs.js\";\nimport type { DelimiterRange } from \"../rules/types.js\";\n\nexport const addAttrs = (\n token: Token | null,\n content: string,\n range: DelimiterRange,\n allowed: (string | RegExp)[],\n): void => {\n if (!token) return;\n\n getAttrs(content, range, allowed).forEach(([key, value]) => {\n switch (key) {\n case \"class\": {\n token.attrJoin(\"class\", value);\n break;\n }\n case \"css-module\": {\n token.attrJoin(\"css-module\", value);\n break;\n }\n default: {\n token.attrPush([key, value]);\n }\n }\n });\n};\n","import { CLASS_MARKER, ID_MARKER } from \"./constants.js\";\nimport type { DelimiterConfig } from \"./types.js\";\nimport type { DelimiterChecker } from \"../rules/types.js\";\n\n/**\n * Get a function to check if a string matches the delimiter pattern\n * 获取一个函数来检查字符串是否匹配分隔符模式\n *\n * @param options - Delimiter configuration / 分隔符配置\n * @param where - Position for matching: start, end, or only / 匹配位置:开始、结束或仅匹配\n * @returns A function that checks if content matches the delimiter pattern / 检查内容是否匹配分隔符模Line truncated
+64
View File
@@ -0,0 +1,64 @@
{
"name": "@mdit/plugin-attrs",
"version": "0.25.1",
"description": "attrs plugin for MarkdownIt",
"keywords": [
"attrs",
"markdown-it",
"markdown-it-plugin"
],
"homepage": "https://mdit-plugins.github.io/attrs.html",
"bugs": {
"url": "https://github.com/mdit-plugins/mdit-plugins/issues"
},
"license": "MIT",
"author": {
"name": "Mr.Hope",
"email": "mister-hope@outlook.com",
"url": "https://mister-hope.com"
},
"repository": {
"type": "git",
"url": "git+https://github.com/mdit-plugins/mdit-plugins.git",
"directory": "packages/attrs"
},
"files": [
"dist"
],
"type": "module",
"sideEffects": false,
"unpkg": "./dist/cdn.umd.js",
"jsdelivr": "./dist/cdn.umd.js",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./package.json": "./package.json"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"@types/markdown-it": "^14.1.2",
"@mdit/helper": "0.23.1"
},
"devDependencies": {
"@mdit/plugin-katex": "0.25.1"
},
"peerDependencies": {
"markdown-it": "^14.1.0"
},
"peerDependenciesMeta": {
"markdown-it": {
"optional": true
}
},
"engines": {
"node": ">= 20"
},
"scripts": {
"build": "tsdown --config-loader unrun",
"clean": "rimraf ./dist"
}
}