@difizen/libro-markdown 0.0.2-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1 -0
- package/es/anchor.d.ts +42 -0
- package/es/anchor.d.ts.map +1 -0
- package/es/anchor.js +153 -0
- package/es/index.d.ts +5 -0
- package/es/index.d.ts.map +1 -0
- package/es/index.js +4 -0
- package/es/markdown-protocol.d.ts +18 -0
- package/es/markdown-protocol.d.ts.map +1 -0
- package/es/markdown-protocol.js +5 -0
- package/es/markdown-render.d.ts +11 -0
- package/es/markdown-render.d.ts.map +1 -0
- package/es/markdown-render.js +48 -0
- package/es/module.d.ts +3 -0
- package/es/module.d.ts.map +1 -0
- package/es/module.js +4 -0
- package/package.json +54 -0
- package/src/anchor.ts +206 -0
- package/src/index.ts +4 -0
- package/src/markdown-protocol.ts +20 -0
- package/src/markdown-render.ts +32 -0
- package/src/module.ts +9 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-present Difizen Team
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# libro shared model
|
package/es/anchor.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { PluginWithOptions } from 'markdown-it';
|
|
2
|
+
import type State from 'markdown-it/lib/rules_core/state_core.js';
|
|
3
|
+
import type Token from 'markdown-it/lib/token.js';
|
|
4
|
+
export type RenderHref = (slug: string, state: State) => string;
|
|
5
|
+
export type RenderAttrs = (slug: string, state: State) => Record<string, string | number>;
|
|
6
|
+
export interface PermalinkOptions {
|
|
7
|
+
class: string;
|
|
8
|
+
symbol: string;
|
|
9
|
+
renderHref: RenderHref;
|
|
10
|
+
renderAttrs: RenderAttrs;
|
|
11
|
+
space: boolean;
|
|
12
|
+
placement: keyof typeof position;
|
|
13
|
+
ariaHidden: boolean;
|
|
14
|
+
}
|
|
15
|
+
export declare function renderHref(slug: string): string;
|
|
16
|
+
export declare function renderAttrs(_slug: string): {};
|
|
17
|
+
declare const position: {
|
|
18
|
+
readonly false: "push";
|
|
19
|
+
readonly true: "unshift";
|
|
20
|
+
readonly after: "push";
|
|
21
|
+
readonly before: "unshift";
|
|
22
|
+
};
|
|
23
|
+
export type PermalinkGenerator = (slug: string, opts: Partial<PermalinkOptions>, state: State, index: number) => void;
|
|
24
|
+
export declare const linkInsideHeader: PermalinkGenerator;
|
|
25
|
+
interface AnchorOptions {
|
|
26
|
+
level: number;
|
|
27
|
+
slugify: (val: string) => string;
|
|
28
|
+
getTokensText(tokens: Token[]): string;
|
|
29
|
+
uniqueSlugStartIndex: number;
|
|
30
|
+
tabIndex: string | false;
|
|
31
|
+
permalink: PermalinkGenerator;
|
|
32
|
+
permalinkOptions: Partial<PermalinkOptions>;
|
|
33
|
+
}
|
|
34
|
+
export declare const slugify: (s: string) => string;
|
|
35
|
+
/**
|
|
36
|
+
* fork from markdown-it-anchor
|
|
37
|
+
* @param md
|
|
38
|
+
* @param options
|
|
39
|
+
*/
|
|
40
|
+
export declare const libroAnchor: PluginWithOptions<Partial<AnchorOptions>>;
|
|
41
|
+
export {};
|
|
42
|
+
//# sourceMappingURL=anchor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anchor.d.ts","sourceRoot":"","sources":["../src/anchor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,KAAK,KAAK,MAAM,0CAA0C,CAAC;AAClE,OAAO,KAAK,KAAK,MAAM,0BAA0B,CAAC;AAElD,MAAM,MAAM,UAAU,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,KAAK,MAAM,CAAC;AAChE,MAAM,MAAM,WAAW,GAAG,CACxB,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,KAAK,KACT,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC;AAErC,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,UAAU,CAAC;IACvB,WAAW,EAAE,WAAW,CAAC;IACzB,KAAK,EAAE,OAAO,CAAC;IACf,SAAS,EAAE,MAAM,OAAO,QAAQ,CAAC;IACjC,UAAU,EAAE,OAAO,CAAC;CACrB;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,UAEtC;AAGD,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,MAExC;AAgBD,QAAA,MAAM,QAAQ;;;;;CAKJ,CAAC;AAEX,MAAM,MAAM,kBAAkB,GAAG,CAC/B,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,OAAO,CAAC,gBAAgB,CAAC,EAC/B,KAAK,EAAE,KAAK,EACZ,KAAK,EAAE,MAAM,KACV,IAAI,CAAC;AAEV,eAAO,MAAM,gBAAgB,EAAE,kBA2B9B,CAAC;AAIF,UAAU,aAAa;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,CAAC;IACjC,aAAa,CAAC,MAAM,EAAE,KAAK,EAAE,GAAG,MAAM,CAAC;IACvC,oBAAoB,EAAE,MAAM,CAAC;IAC7B,QAAQ,EAAE,MAAM,GAAG,KAAK,CAAC;IACzB,SAAS,EAAE,kBAAkB,CAAC;IAC9B,gBAAgB,EAAE,OAAO,CAAC,gBAAgB,CAAC,CAAC;CAC7C;AAED,eAAO,MAAM,OAAO,MAAO,MAAM,WACwC,CAAC;AAiD1E;;;;GAIG;AACH,eAAO,MAAM,WAAW,EAAE,iBAAiB,CAAC,OAAO,CAAC,aAAa,CAAC,CAoDjE,CAAC"}
|
package/es/anchor.js
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
2
|
+
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
3
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
4
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
5
|
+
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
6
|
+
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
7
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
8
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
9
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
10
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
11
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
12
|
+
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
13
|
+
export function renderHref(slug) {
|
|
14
|
+
return "#".concat(slug);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
18
|
+
export function renderAttrs(_slug) {
|
|
19
|
+
return {};
|
|
20
|
+
}
|
|
21
|
+
var commonDefaults = {
|
|
22
|
+
class: 'header-anchor',
|
|
23
|
+
symbol: '#',
|
|
24
|
+
renderHref: renderHref,
|
|
25
|
+
renderAttrs: renderAttrs,
|
|
26
|
+
space: true,
|
|
27
|
+
placement: 'after',
|
|
28
|
+
ariaHidden: false
|
|
29
|
+
};
|
|
30
|
+
var permalinkSymbolMeta = {
|
|
31
|
+
isPermalinkSymbol: true
|
|
32
|
+
};
|
|
33
|
+
var position = {
|
|
34
|
+
false: 'push',
|
|
35
|
+
true: 'unshift',
|
|
36
|
+
after: 'push',
|
|
37
|
+
before: 'unshift'
|
|
38
|
+
};
|
|
39
|
+
export var linkInsideHeader = function linkInsideHeader(slug, options, state, idx) {
|
|
40
|
+
var _state$tokens$childre2;
|
|
41
|
+
var opts = _objectSpread(_objectSpread({}, commonDefaults), options);
|
|
42
|
+
var linkTokens = [Object.assign(new state.Token('link_open', 'a', 1), {
|
|
43
|
+
attrs: [].concat(_toConsumableArray(opts.class ? [['class', opts.class]] : []), [['href', opts.renderHref(slug, state)]], _toConsumableArray(opts.ariaHidden ? [['aria-hidden', 'true']] : []), _toConsumableArray(Object.entries(opts.renderAttrs(slug, state))))
|
|
44
|
+
}), Object.assign(new state.Token('html_inline', '', 0), {
|
|
45
|
+
content: opts.symbol,
|
|
46
|
+
meta: permalinkSymbolMeta
|
|
47
|
+
}), new state.Token('link_close', 'a', -1)];
|
|
48
|
+
if (opts.space) {
|
|
49
|
+
var _state$tokens$childre;
|
|
50
|
+
var space = typeof opts.space === 'string' ? opts.space : ' ';
|
|
51
|
+
var type = typeof opts.space === 'string' ? 'html_inline' : 'text';
|
|
52
|
+
(_state$tokens$childre = state.tokens[idx + 1].children) === null || _state$tokens$childre === void 0 ? void 0 : _state$tokens$childre[position[opts.placement]](Object.assign(new state.Token(type, '', 0), {
|
|
53
|
+
content: space
|
|
54
|
+
}));
|
|
55
|
+
}
|
|
56
|
+
(_state$tokens$childre2 = state.tokens[idx + 1].children) === null || _state$tokens$childre2 === void 0 ? void 0 : _state$tokens$childre2[position[opts.placement]].apply(_state$tokens$childre2, linkTokens);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
//// anchor
|
|
60
|
+
|
|
61
|
+
export var slugify = function slugify(s) {
|
|
62
|
+
return encodeURIComponent(String(s).trim().toLowerCase().replace(/\s+/g, '-'));
|
|
63
|
+
};
|
|
64
|
+
function getTokensText(tokens) {
|
|
65
|
+
return tokens.filter(function (t) {
|
|
66
|
+
return ['text', 'code_inline'].includes(t.type);
|
|
67
|
+
}).map(function (t) {
|
|
68
|
+
return t.content;
|
|
69
|
+
}).join('');
|
|
70
|
+
}
|
|
71
|
+
function uniqueSlug(slug, slugs, failOnNonUnique, startIndex) {
|
|
72
|
+
var uniq = slug;
|
|
73
|
+
var i = startIndex;
|
|
74
|
+
if (failOnNonUnique && Object.prototype.hasOwnProperty.call(slugs, uniq)) {
|
|
75
|
+
throw new Error("User defined `id` attribute `".concat(slug, "` is not unique. Please fix it in your Markdown to continue."));
|
|
76
|
+
} else {
|
|
77
|
+
while (Object.prototype.hasOwnProperty.call(slugs, uniq)) {
|
|
78
|
+
uniq = "".concat(slug, "-").concat(i);
|
|
79
|
+
i += 1;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
slugs[uniq] = true;
|
|
83
|
+
return uniq;
|
|
84
|
+
}
|
|
85
|
+
var isLevelSelectedNumber = function isLevelSelectedNumber(selection) {
|
|
86
|
+
return function (level) {
|
|
87
|
+
return level >= selection;
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
var isLevelSelectedArray = function isLevelSelectedArray(selection) {
|
|
91
|
+
return function (level) {
|
|
92
|
+
return selection.includes(level);
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
var defaultOptions = {
|
|
96
|
+
level: 1,
|
|
97
|
+
slugify: slugify,
|
|
98
|
+
uniqueSlugStartIndex: 1,
|
|
99
|
+
tabIndex: '-1',
|
|
100
|
+
getTokensText: getTokensText,
|
|
101
|
+
permalink: linkInsideHeader,
|
|
102
|
+
permalinkOptions: {}
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* fork from markdown-it-anchor
|
|
107
|
+
* @param md
|
|
108
|
+
* @param options
|
|
109
|
+
*/
|
|
110
|
+
export var libroAnchor = function libroAnchor(md, options) {
|
|
111
|
+
var opts = _objectSpread(_objectSpread({}, defaultOptions), options);
|
|
112
|
+
md.core.ruler.push('anchor', function (state) {
|
|
113
|
+
var _state$env;
|
|
114
|
+
var slugs = {};
|
|
115
|
+
var tokens = state.tokens;
|
|
116
|
+
var cellId = (_state$env = state.env) === null || _state$env === void 0 ? void 0 : _state$env.cellId;
|
|
117
|
+
var isLevelSelected = Array.isArray(opts.level) ? isLevelSelectedArray(opts.level) : isLevelSelectedNumber(opts.level);
|
|
118
|
+
for (var idx = 0; idx < tokens.length; idx++) {
|
|
119
|
+
var _tokens$children;
|
|
120
|
+
var token = tokens[idx];
|
|
121
|
+
if (token.type !== 'heading_open') {
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (!isLevelSelected(Number(token.tag.substr(1)))) {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Aggregate the next token children text.
|
|
129
|
+
var title = opts.getTokensText((_tokens$children = tokens[idx + 1].children) !== null && _tokens$children !== void 0 ? _tokens$children : []);
|
|
130
|
+
var _slug2 = token.attrGet('id');
|
|
131
|
+
if (_slug2 === null) {
|
|
132
|
+
_slug2 = uniqueSlug(opts.slugify(title), slugs, false, opts.uniqueSlugStartIndex);
|
|
133
|
+
} else {
|
|
134
|
+
_slug2 = uniqueSlug(_slug2, slugs, true, opts.uniqueSlugStartIndex);
|
|
135
|
+
}
|
|
136
|
+
if (cellId) {
|
|
137
|
+
token.attrSet('id', "".concat(cellId, "-").concat(_slug2));
|
|
138
|
+
} else {
|
|
139
|
+
token.attrSet('id', _slug2);
|
|
140
|
+
}
|
|
141
|
+
if (opts.tabIndex !== false) {
|
|
142
|
+
token.attrSet('tabindex', "".concat(opts.tabIndex));
|
|
143
|
+
}
|
|
144
|
+
if (typeof opts.permalink === 'function') {
|
|
145
|
+
opts.permalink(_slug2, opts.permalinkOptions, state, idx);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// A permalink renderer could modify the `tokens` array so
|
|
149
|
+
// make sure to get the up-to-date index on each iteration.
|
|
150
|
+
idx = tokens.indexOf(token);
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
};
|
package/es/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,wBAAwB,CAAC;AACvC,cAAc,sBAAsB,CAAC;AACrC,cAAc,aAAa,CAAC"}
|
package/es/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare const MarkdownParser: unique symbol;
|
|
2
|
+
export interface MarkdownRenderOption {
|
|
3
|
+
cellId?: string;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* The interface for a Markdown parser.
|
|
7
|
+
*/
|
|
8
|
+
export interface MarkdownParser {
|
|
9
|
+
/**
|
|
10
|
+
* Render a markdown source.
|
|
11
|
+
*
|
|
12
|
+
* @param source - The string to render.
|
|
13
|
+
* @returns - string.
|
|
14
|
+
*/
|
|
15
|
+
render(source: string, options?: MarkdownRenderOption): string;
|
|
16
|
+
slugify: (val: string) => string;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=markdown-protocol.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"markdown-protocol.d.ts","sourceRoot":"","sources":["../src/markdown-protocol.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,cAAc,eAA2B,CAAC;AAEvD,MAAM,WAAW,oBAAoB;IACnC,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;;;;OAKG;IACH,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,oBAAoB,GAAG,MAAM,CAAC;IAE/D,OAAO,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,CAAC;CAClC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import MarkdownIt from 'markdown-it';
|
|
2
|
+
import type { MarkdownRenderOption } from './markdown-protocol.js';
|
|
3
|
+
import { MarkdownParser } from './markdown-protocol.js';
|
|
4
|
+
export declare class MarkdownRender implements MarkdownParser {
|
|
5
|
+
protected mkt: MarkdownIt;
|
|
6
|
+
slugify: (s: string) => string;
|
|
7
|
+
enablePermalink: boolean;
|
|
8
|
+
init(): void;
|
|
9
|
+
render(markdownText: string, options?: MarkdownRenderOption): string;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=markdown-render.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"markdown-render.d.ts","sourceRoot":"","sources":["../src/markdown-render.ts"],"names":[],"mappings":"AACA,OAAO,UAAU,MAAM,aAAa,CAAC;AAGrC,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AACnE,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAExD,qBACa,cAAe,YAAW,cAAc;IACnD,SAAS,CAAC,GAAG,EAAE,UAAU,CAAC;IAC1B,OAAO,wBAAW;IAClB,eAAe,UAAS;IAGxB,IAAI;IAaJ,MAAM,CAAC,YAAY,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,oBAAoB,GAAG,MAAM;CAIrE"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
2
|
+
var _dec, _dec2, _class, _class2;
|
|
3
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
4
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
5
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
6
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
7
|
+
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
8
|
+
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; }
|
|
9
|
+
import { postConstruct, singleton } from '@difizen/mana-app';
|
|
10
|
+
import MarkdownIt from 'markdown-it';
|
|
11
|
+
import { libroAnchor, linkInsideHeader, slugify } from "./anchor.js";
|
|
12
|
+
import { MarkdownParser } from "./markdown-protocol.js";
|
|
13
|
+
export var MarkdownRender = (_dec = singleton({
|
|
14
|
+
token: MarkdownParser
|
|
15
|
+
}), _dec2 = postConstruct(), _dec(_class = (_class2 = /*#__PURE__*/function () {
|
|
16
|
+
function MarkdownRender() {
|
|
17
|
+
_classCallCheck(this, MarkdownRender);
|
|
18
|
+
this.slugify = slugify;
|
|
19
|
+
this.enablePermalink = false;
|
|
20
|
+
}
|
|
21
|
+
_createClass(MarkdownRender, [{
|
|
22
|
+
key: "init",
|
|
23
|
+
value: function init() {
|
|
24
|
+
this.mkt = new MarkdownIt({
|
|
25
|
+
html: true,
|
|
26
|
+
linkify: true
|
|
27
|
+
});
|
|
28
|
+
this.mkt.linkify.set({
|
|
29
|
+
fuzzyLink: false
|
|
30
|
+
});
|
|
31
|
+
this.mkt.use(libroAnchor, {
|
|
32
|
+
permalinkOptions: {
|
|
33
|
+
class: 'libro-InternalAnchorLink',
|
|
34
|
+
space: false
|
|
35
|
+
},
|
|
36
|
+
permalink: this.enablePermalink ? linkInsideHeader : false,
|
|
37
|
+
slugify: this.slugify
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}, {
|
|
41
|
+
key: "render",
|
|
42
|
+
value: function render(markdownText, options) {
|
|
43
|
+
var unsanitizedRenderedMarkdown = this.mkt.render(markdownText, options);
|
|
44
|
+
return unsanitizedRenderedMarkdown;
|
|
45
|
+
}
|
|
46
|
+
}]);
|
|
47
|
+
return MarkdownRender;
|
|
48
|
+
}(), (_applyDecoratedDescriptor(_class2.prototype, "init", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "init"), _class2.prototype)), _class2)) || _class);
|
package/es/module.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../src/module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAK/C,eAAO,MAAM,cAAc,YAG1B,CAAC"}
|
package/es/module.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@difizen/libro-markdown",
|
|
3
|
+
"version": "0.0.2-alpha.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"libro",
|
|
7
|
+
"notebook",
|
|
8
|
+
"toc"
|
|
9
|
+
],
|
|
10
|
+
"repository": "git@github.com:difizen/libro.git",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"typings": "./es/index.d.ts",
|
|
16
|
+
"default": "./es/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./mock": {
|
|
19
|
+
"typings": "./es/mock/index.d.ts",
|
|
20
|
+
"default": "./es/mock/index.js"
|
|
21
|
+
},
|
|
22
|
+
"./es/mock": {
|
|
23
|
+
"typings": "./es/mock/index.d.ts",
|
|
24
|
+
"default": "./es/mock/index.js"
|
|
25
|
+
},
|
|
26
|
+
"./package.json": "./package.json"
|
|
27
|
+
},
|
|
28
|
+
"main": "es/index.js",
|
|
29
|
+
"module": "es/index.js",
|
|
30
|
+
"typings": "es/index.d.ts",
|
|
31
|
+
"files": [
|
|
32
|
+
"es",
|
|
33
|
+
"src"
|
|
34
|
+
],
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@difizen/mana-app": "alpha",
|
|
37
|
+
"@types/markdown-it": "^12.2.3",
|
|
38
|
+
"markdown-it": "^13.0.1",
|
|
39
|
+
"markdown-it-anchor": "^8.6.5"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"setup": "father build",
|
|
43
|
+
"build": "father build",
|
|
44
|
+
"test": ": Note: lint task is delegated to test:* scripts",
|
|
45
|
+
"test:vitest": "vitest run",
|
|
46
|
+
"test:jest": "jest",
|
|
47
|
+
"coverage": ": Note: lint task is delegated to coverage:* scripts",
|
|
48
|
+
"coverage:vitest": "vitest run --coverage",
|
|
49
|
+
"coverage:jest": "jest --coverage",
|
|
50
|
+
"lint": ": Note: lint task is delegated to lint:* scripts",
|
|
51
|
+
"lint:eslint": "eslint src",
|
|
52
|
+
"lint:tsc": "tsc --noEmit"
|
|
53
|
+
}
|
|
54
|
+
}
|
package/src/anchor.ts
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import type { PluginWithOptions } from 'markdown-it';
|
|
2
|
+
import type State from 'markdown-it/lib/rules_core/state_core.js';
|
|
3
|
+
import type Token from 'markdown-it/lib/token.js';
|
|
4
|
+
|
|
5
|
+
export type RenderHref = (slug: string, state: State) => string;
|
|
6
|
+
export type RenderAttrs = (
|
|
7
|
+
slug: string,
|
|
8
|
+
state: State,
|
|
9
|
+
) => Record<string, string | number>;
|
|
10
|
+
|
|
11
|
+
export interface PermalinkOptions {
|
|
12
|
+
class: string;
|
|
13
|
+
symbol: string;
|
|
14
|
+
renderHref: RenderHref;
|
|
15
|
+
renderAttrs: RenderAttrs;
|
|
16
|
+
space: boolean;
|
|
17
|
+
placement: keyof typeof position;
|
|
18
|
+
ariaHidden: boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function renderHref(slug: string) {
|
|
22
|
+
return `#${slug}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
26
|
+
export function renderAttrs(_slug: string) {
|
|
27
|
+
return {};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const commonDefaults: PermalinkOptions = {
|
|
31
|
+
class: 'header-anchor',
|
|
32
|
+
symbol: '#',
|
|
33
|
+
renderHref,
|
|
34
|
+
renderAttrs,
|
|
35
|
+
space: true,
|
|
36
|
+
placement: 'after',
|
|
37
|
+
ariaHidden: false,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const permalinkSymbolMeta = {
|
|
41
|
+
isPermalinkSymbol: true,
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const position = {
|
|
45
|
+
false: 'push',
|
|
46
|
+
true: 'unshift',
|
|
47
|
+
after: 'push',
|
|
48
|
+
before: 'unshift',
|
|
49
|
+
} as const;
|
|
50
|
+
|
|
51
|
+
export type PermalinkGenerator = (
|
|
52
|
+
slug: string,
|
|
53
|
+
opts: Partial<PermalinkOptions>,
|
|
54
|
+
state: State,
|
|
55
|
+
index: number,
|
|
56
|
+
) => void;
|
|
57
|
+
|
|
58
|
+
export const linkInsideHeader: PermalinkGenerator = (slug, options, state, idx) => {
|
|
59
|
+
const opts = { ...commonDefaults, ...options };
|
|
60
|
+
const linkTokens = [
|
|
61
|
+
Object.assign(new state.Token('link_open', 'a', 1), {
|
|
62
|
+
attrs: [
|
|
63
|
+
...(opts.class ? [['class', opts.class]] : []),
|
|
64
|
+
['href', opts.renderHref(slug, state)],
|
|
65
|
+
...(opts.ariaHidden ? [['aria-hidden', 'true']] : []),
|
|
66
|
+
...Object.entries(opts.renderAttrs(slug, state)),
|
|
67
|
+
],
|
|
68
|
+
}),
|
|
69
|
+
Object.assign(new state.Token('html_inline', '', 0), {
|
|
70
|
+
content: opts.symbol,
|
|
71
|
+
meta: permalinkSymbolMeta,
|
|
72
|
+
}),
|
|
73
|
+
new state.Token('link_close', 'a', -1),
|
|
74
|
+
];
|
|
75
|
+
|
|
76
|
+
if (opts.space) {
|
|
77
|
+
const space = typeof opts.space === 'string' ? opts.space : ' ';
|
|
78
|
+
const type = typeof opts.space === 'string' ? 'html_inline' : 'text';
|
|
79
|
+
state.tokens[idx + 1].children?.[position[opts.placement]](
|
|
80
|
+
Object.assign(new state.Token(type, '', 0), { content: space }),
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
state.tokens[idx + 1].children?.[position[opts.placement]](...linkTokens);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
//// anchor
|
|
88
|
+
|
|
89
|
+
interface AnchorOptions {
|
|
90
|
+
level: number;
|
|
91
|
+
slugify: (val: string) => string;
|
|
92
|
+
getTokensText(tokens: Token[]): string;
|
|
93
|
+
uniqueSlugStartIndex: number;
|
|
94
|
+
tabIndex: string | false;
|
|
95
|
+
permalink: PermalinkGenerator;
|
|
96
|
+
permalinkOptions: Partial<PermalinkOptions>;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export const slugify = (s: string) =>
|
|
100
|
+
encodeURIComponent(String(s).trim().toLowerCase().replace(/\s+/g, '-'));
|
|
101
|
+
|
|
102
|
+
function getTokensText(tokens: Token[]) {
|
|
103
|
+
return tokens
|
|
104
|
+
.filter((t) => ['text', 'code_inline'].includes(t.type))
|
|
105
|
+
.map((t) => t.content)
|
|
106
|
+
.join('');
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function uniqueSlug(
|
|
110
|
+
slug: string,
|
|
111
|
+
slugs: Record<string, boolean>,
|
|
112
|
+
failOnNonUnique: boolean,
|
|
113
|
+
startIndex: number,
|
|
114
|
+
) {
|
|
115
|
+
let uniq = slug;
|
|
116
|
+
let i = startIndex;
|
|
117
|
+
|
|
118
|
+
if (failOnNonUnique && Object.prototype.hasOwnProperty.call(slugs, uniq)) {
|
|
119
|
+
throw new Error(
|
|
120
|
+
`User defined \`id\` attribute \`${slug}\` is not unique. Please fix it in your Markdown to continue.`,
|
|
121
|
+
);
|
|
122
|
+
} else {
|
|
123
|
+
while (Object.prototype.hasOwnProperty.call(slugs, uniq)) {
|
|
124
|
+
uniq = `${slug}-${i}`;
|
|
125
|
+
i += 1;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
slugs[uniq] = true;
|
|
130
|
+
|
|
131
|
+
return uniq;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const isLevelSelectedNumber = (selection: number) => (level: number) =>
|
|
135
|
+
level >= selection;
|
|
136
|
+
const isLevelSelectedArray = (selection: number[]) => (level: number) =>
|
|
137
|
+
selection.includes(level);
|
|
138
|
+
|
|
139
|
+
const defaultOptions: AnchorOptions = {
|
|
140
|
+
level: 1,
|
|
141
|
+
slugify,
|
|
142
|
+
uniqueSlugStartIndex: 1,
|
|
143
|
+
tabIndex: '-1',
|
|
144
|
+
getTokensText,
|
|
145
|
+
permalink: linkInsideHeader,
|
|
146
|
+
permalinkOptions: {},
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* fork from markdown-it-anchor
|
|
151
|
+
* @param md
|
|
152
|
+
* @param options
|
|
153
|
+
*/
|
|
154
|
+
export const libroAnchor: PluginWithOptions<Partial<AnchorOptions>> = (md, options) => {
|
|
155
|
+
const opts = { ...defaultOptions, ...options };
|
|
156
|
+
md.core.ruler.push('anchor', (state) => {
|
|
157
|
+
const slugs: Record<string, boolean> = {};
|
|
158
|
+
const tokens = state.tokens;
|
|
159
|
+
const cellId = state.env?.cellId as string | undefined;
|
|
160
|
+
|
|
161
|
+
const isLevelSelected = Array.isArray(opts.level)
|
|
162
|
+
? isLevelSelectedArray(opts.level)
|
|
163
|
+
: isLevelSelectedNumber(opts.level);
|
|
164
|
+
|
|
165
|
+
for (let idx = 0; idx < tokens.length; idx++) {
|
|
166
|
+
const token = tokens[idx];
|
|
167
|
+
|
|
168
|
+
if (token.type !== 'heading_open') {
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (!isLevelSelected(Number(token.tag.substr(1)))) {
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Aggregate the next token children text.
|
|
177
|
+
const title = opts.getTokensText(tokens[idx + 1].children ?? []);
|
|
178
|
+
|
|
179
|
+
let slug = token.attrGet('id');
|
|
180
|
+
|
|
181
|
+
if (slug === null) {
|
|
182
|
+
slug = uniqueSlug(opts.slugify(title), slugs, false, opts.uniqueSlugStartIndex);
|
|
183
|
+
} else {
|
|
184
|
+
slug = uniqueSlug(slug, slugs, true, opts.uniqueSlugStartIndex);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (cellId) {
|
|
188
|
+
token.attrSet('id', `${cellId}-${slug}`);
|
|
189
|
+
} else {
|
|
190
|
+
token.attrSet('id', slug);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (opts.tabIndex !== false) {
|
|
194
|
+
token.attrSet('tabindex', `${opts.tabIndex}`);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (typeof opts.permalink === 'function') {
|
|
198
|
+
opts.permalink(slug, opts.permalinkOptions, state, idx);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// A permalink renderer could modify the `tokens` array so
|
|
202
|
+
// make sure to get the up-to-date index on each iteration.
|
|
203
|
+
idx = tokens.indexOf(token);
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export const MarkdownParser = Symbol('MarkdownParser');
|
|
2
|
+
|
|
3
|
+
export interface MarkdownRenderOption {
|
|
4
|
+
cellId?: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The interface for a Markdown parser.
|
|
9
|
+
*/
|
|
10
|
+
export interface MarkdownParser {
|
|
11
|
+
/**
|
|
12
|
+
* Render a markdown source.
|
|
13
|
+
*
|
|
14
|
+
* @param source - The string to render.
|
|
15
|
+
* @returns - string.
|
|
16
|
+
*/
|
|
17
|
+
render(source: string, options?: MarkdownRenderOption): string;
|
|
18
|
+
|
|
19
|
+
slugify: (val: string) => string;
|
|
20
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { postConstruct, singleton } from '@difizen/mana-app';
|
|
2
|
+
import MarkdownIt from 'markdown-it';
|
|
3
|
+
|
|
4
|
+
import { libroAnchor, linkInsideHeader, slugify } from './anchor.js';
|
|
5
|
+
import type { MarkdownRenderOption } from './markdown-protocol.js';
|
|
6
|
+
import { MarkdownParser } from './markdown-protocol.js';
|
|
7
|
+
|
|
8
|
+
@singleton({ token: MarkdownParser })
|
|
9
|
+
export class MarkdownRender implements MarkdownParser {
|
|
10
|
+
protected mkt: MarkdownIt;
|
|
11
|
+
slugify = slugify;
|
|
12
|
+
enablePermalink = false;
|
|
13
|
+
|
|
14
|
+
@postConstruct()
|
|
15
|
+
init() {
|
|
16
|
+
this.mkt = new MarkdownIt({
|
|
17
|
+
html: true,
|
|
18
|
+
linkify: true,
|
|
19
|
+
});
|
|
20
|
+
this.mkt.linkify.set({ fuzzyLink: false });
|
|
21
|
+
this.mkt.use(libroAnchor, {
|
|
22
|
+
permalinkOptions: { class: 'libro-InternalAnchorLink', space: false },
|
|
23
|
+
permalink: this.enablePermalink ? linkInsideHeader : false,
|
|
24
|
+
slugify: this.slugify,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
render(markdownText: string, options?: MarkdownRenderOption): string {
|
|
29
|
+
const unsanitizedRenderedMarkdown = this.mkt.render(markdownText, options);
|
|
30
|
+
return unsanitizedRenderedMarkdown;
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/module.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ManaModule } from '@difizen/mana-app';
|
|
2
|
+
|
|
3
|
+
import { MarkdownParser } from './markdown-protocol.js';
|
|
4
|
+
import { MarkdownRender } from './markdown-render.js';
|
|
5
|
+
|
|
6
|
+
export const MarkdownModule = ManaModule.create().register(
|
|
7
|
+
MarkdownParser,
|
|
8
|
+
MarkdownRender,
|
|
9
|
+
);
|