@difizen/libro-markdown 0.0.0-snapshot-20241017072317
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/config-contribution.d.ts +5 -0
- package/es/config-contribution.d.ts.map +1 -0
- package/es/config-contribution.js +24 -0
- package/es/config.d.ts +6 -0
- package/es/config.d.ts.map +1 -0
- package/es/config.js +14 -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 +14 -0
- package/es/markdown-render.d.ts.map +1 -0
- package/es/markdown-render.js +91 -0
- package/es/module.d.ts +3 -0
- package/es/module.d.ts.map +1 -0
- package/es/module.js +5 -0
- package/package.json +57 -0
- package/src/anchor.ts +206 -0
- package/src/config-contribution.ts +11 -0
- package/src/config.ts +17 -0
- package/src/index.spec.ts +10 -0
- package/src/index.ts +4 -0
- package/src/markdown-protocol.ts +20 -0
- package/src/markdown-render.ts +85 -0
- package/src/module.ts +11 -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,IAAI,EAAE,MAAM,MAEvC;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(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
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(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
9
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
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(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
12
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
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 || _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 || _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 _slug = token.attrGet('id');
|
|
131
|
+
if (_slug === null) {
|
|
132
|
+
_slug = uniqueSlug(opts.slugify(title), slugs, false, opts.uniqueSlugStartIndex);
|
|
133
|
+
} else {
|
|
134
|
+
_slug = uniqueSlug(_slug, slugs, true, opts.uniqueSlugStartIndex);
|
|
135
|
+
}
|
|
136
|
+
if (cellId) {
|
|
137
|
+
token.attrSet('id', "".concat(cellId, "-").concat(_slug));
|
|
138
|
+
} else {
|
|
139
|
+
token.attrSet('id', _slug);
|
|
140
|
+
}
|
|
141
|
+
if (opts.tabIndex !== false) {
|
|
142
|
+
token.attrSet('tabindex', "".concat(opts.tabIndex));
|
|
143
|
+
}
|
|
144
|
+
if (typeof opts.permalink === 'function') {
|
|
145
|
+
opts.permalink(_slug, 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
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { ConfigurationContribution } from '@difizen/mana-app';
|
|
2
|
+
export declare class LibroMarkdownSettingContribution implements ConfigurationContribution {
|
|
3
|
+
registerConfigurations(): import("@difizen/mana-app").ConfigurationNode<boolean>[];
|
|
4
|
+
}
|
|
5
|
+
//# sourceMappingURL=config-contribution.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config-contribution.d.ts","sourceRoot":"","sources":["../src/config-contribution.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,mBAAmB,CAAC;AAK9D,qBACa,gCAAiC,YAAW,yBAAyB;IAChF,sBAAsB;CAGvB"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
+
var _dec, _class;
|
|
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(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
7
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
8
|
+
import { ConfigurationContribution } from '@difizen/mana-app';
|
|
9
|
+
import { singleton } from '@difizen/mana-app';
|
|
10
|
+
import { LibroMarkdownConfiguration } from "./config.js";
|
|
11
|
+
export var LibroMarkdownSettingContribution = (_dec = singleton({
|
|
12
|
+
contrib: ConfigurationContribution
|
|
13
|
+
}), _dec(_class = /*#__PURE__*/function () {
|
|
14
|
+
function LibroMarkdownSettingContribution() {
|
|
15
|
+
_classCallCheck(this, LibroMarkdownSettingContribution);
|
|
16
|
+
}
|
|
17
|
+
_createClass(LibroMarkdownSettingContribution, [{
|
|
18
|
+
key: "registerConfigurations",
|
|
19
|
+
value: function registerConfigurations() {
|
|
20
|
+
return [LibroMarkdownConfiguration.TargetToBlank];
|
|
21
|
+
}
|
|
22
|
+
}]);
|
|
23
|
+
return LibroMarkdownSettingContribution;
|
|
24
|
+
}()) || _class);
|
package/es/config.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ConfigurationNode } from '@difizen/mana-app';
|
|
2
|
+
export declare const LibroConfigLinkTargetToBlank: ConfigurationNode<boolean>;
|
|
3
|
+
export declare const LibroMarkdownConfiguration: {
|
|
4
|
+
TargetToBlank: ConfigurationNode<boolean>;
|
|
5
|
+
};
|
|
6
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAG3D,eAAO,MAAM,4BAA4B,EAAE,iBAAiB,CAAC,OAAO,CASnE,CAAC;AAEF,eAAO,MAAM,0BAA0B;;CAEtC,CAAC"}
|
package/es/config.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { l10n } from '@difizen/mana-l10n';
|
|
2
|
+
export var LibroConfigLinkTargetToBlank = {
|
|
3
|
+
id: 'libro.markdown.link.target.to.blank',
|
|
4
|
+
description: l10n.t('markdown的a标签是否另起一页'),
|
|
5
|
+
title: 'a标签另起一页',
|
|
6
|
+
type: 'checkbox',
|
|
7
|
+
defaultValue: true,
|
|
8
|
+
schema: {
|
|
9
|
+
type: 'boolean'
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
export var LibroMarkdownConfiguration = {
|
|
13
|
+
TargetToBlank: LibroConfigLinkTargetToBlank
|
|
14
|
+
};
|
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,14 @@
|
|
|
1
|
+
import { ConfigurationService } from '@difizen/mana-app';
|
|
2
|
+
import MarkdownIt from 'markdown-it';
|
|
3
|
+
import type { MarkdownRenderOption } from './markdown-protocol.js';
|
|
4
|
+
import { MarkdownParser } from './markdown-protocol.js';
|
|
5
|
+
import 'katex/dist/katex.min.css';
|
|
6
|
+
export declare class MarkdownRender implements MarkdownParser {
|
|
7
|
+
protected mkt: MarkdownIt;
|
|
8
|
+
slugify: (s: string) => string;
|
|
9
|
+
enablePermalink: boolean;
|
|
10
|
+
protected configurationService: ConfigurationService;
|
|
11
|
+
init(): void;
|
|
12
|
+
render(markdownText: string, options?: MarkdownRenderOption): string;
|
|
13
|
+
}
|
|
14
|
+
//# 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":"AAAA,OAAO,EACL,oBAAoB,EAIrB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,UAAU,MAAM,aAAa,CAAC;AAIrC,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AACnE,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AACxD,OAAO,0BAA0B,CAAC;AAElC,qBACa,cAAe,YAAW,cAAc;IACnD,SAAS,CAAC,GAAG,EAAE,UAAU,CAAC;IAC1B,OAAO,wBAAW;IAClB,eAAe,UAAS;IACM,SAAS,CAAC,oBAAoB,EAAE,oBAAoB,CAAC;IAGnF,IAAI;IAyDJ,MAAM,CAAC,YAAY,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,oBAAoB,GAAG,MAAM;CAIrE"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
+
var _dec, _dec2, _dec3, _class, _class2, _descriptor;
|
|
3
|
+
function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); }
|
|
4
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
5
|
+
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); } }
|
|
6
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
7
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
8
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
9
|
+
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; }
|
|
10
|
+
function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'transform-class-properties is enabled and runs after the decorators transform.'); }
|
|
11
|
+
import { ConfigurationService, inject, postConstruct, singleton } from '@difizen/mana-app';
|
|
12
|
+
import latexPlugin from '@traptitech/markdown-it-katex';
|
|
13
|
+
import MarkdownIt from 'markdown-it';
|
|
14
|
+
import { libroAnchor, linkInsideHeader, slugify } from "./anchor.js";
|
|
15
|
+
import { LibroMarkdownConfiguration } from "./config.js";
|
|
16
|
+
import { MarkdownParser } from "./markdown-protocol.js";
|
|
17
|
+
import 'katex/dist/katex.min.css';
|
|
18
|
+
export var MarkdownRender = (_dec = singleton({
|
|
19
|
+
token: MarkdownParser
|
|
20
|
+
}), _dec2 = inject(ConfigurationService), _dec3 = postConstruct(), _dec(_class = (_class2 = /*#__PURE__*/function () {
|
|
21
|
+
function MarkdownRender() {
|
|
22
|
+
_classCallCheck(this, MarkdownRender);
|
|
23
|
+
this.slugify = slugify;
|
|
24
|
+
this.enablePermalink = false;
|
|
25
|
+
_initializerDefineProperty(this, "configurationService", _descriptor, this);
|
|
26
|
+
}
|
|
27
|
+
_createClass(MarkdownRender, [{
|
|
28
|
+
key: "init",
|
|
29
|
+
value: function init() {
|
|
30
|
+
var _this = this;
|
|
31
|
+
this.mkt = new MarkdownIt({
|
|
32
|
+
html: true,
|
|
33
|
+
linkify: true
|
|
34
|
+
});
|
|
35
|
+
this.mkt.linkify.set({
|
|
36
|
+
fuzzyLink: false
|
|
37
|
+
});
|
|
38
|
+
this.mkt.use(libroAnchor, {
|
|
39
|
+
permalinkOptions: {
|
|
40
|
+
class: 'libro-InternalAnchorLink',
|
|
41
|
+
space: false
|
|
42
|
+
},
|
|
43
|
+
permalink: this.enablePermalink ? linkInsideHeader : false,
|
|
44
|
+
slugify: this.slugify
|
|
45
|
+
});
|
|
46
|
+
this.mkt.use(latexPlugin);
|
|
47
|
+
this.configurationService.get(LibroMarkdownConfiguration.TargetToBlank).then(function (value) {
|
|
48
|
+
if (value) {
|
|
49
|
+
_this.mkt.use(function (md) {
|
|
50
|
+
var defaultRender = md.renderer.rules['link_open'] || function (tokens, idx, options, env, self) {
|
|
51
|
+
return self.renderToken(tokens, idx, options);
|
|
52
|
+
};
|
|
53
|
+
md.renderer.rules['link_open'] = function (tokens, idx, options, env, self) {
|
|
54
|
+
// 获取当前的token
|
|
55
|
+
var token = tokens[idx];
|
|
56
|
+
// 检查是否已有target属性
|
|
57
|
+
var targetIndex = token.attrIndex('target');
|
|
58
|
+
if (targetIndex < 0) {
|
|
59
|
+
// 如果没有target属性,添加target="_blank"
|
|
60
|
+
token.attrPush(['target', '_blank']);
|
|
61
|
+
} else {
|
|
62
|
+
// 如果已有target属性,修改为target="_blank"
|
|
63
|
+
if (token.attrs !== null) {
|
|
64
|
+
token.attrs[targetIndex][1] = '_blank';
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// 调用默认的渲染函数
|
|
69
|
+
return defaultRender(tokens, idx, options, env, self);
|
|
70
|
+
};
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
return;
|
|
74
|
+
}).catch(function () {
|
|
75
|
+
//
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}, {
|
|
79
|
+
key: "render",
|
|
80
|
+
value: function render(markdownText, options) {
|
|
81
|
+
var unsanitizedRenderedMarkdown = this.mkt.render(markdownText, options);
|
|
82
|
+
return unsanitizedRenderedMarkdown;
|
|
83
|
+
}
|
|
84
|
+
}]);
|
|
85
|
+
return MarkdownRender;
|
|
86
|
+
}(), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "configurationService", [_dec2], {
|
|
87
|
+
configurable: true,
|
|
88
|
+
enumerable: true,
|
|
89
|
+
writable: true,
|
|
90
|
+
initializer: null
|
|
91
|
+
}), _applyDecoratedDescriptor(_class2.prototype, "init", [_dec3], 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;AAM/C,eAAO,MAAM,cAAc,YAI1B,CAAC"}
|
package/es/module.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { ManaModule } from '@difizen/mana-app';
|
|
2
|
+
import { LibroMarkdownSettingContribution } from "./config-contribution.js";
|
|
3
|
+
import { MarkdownParser } from "./markdown-protocol.js";
|
|
4
|
+
import { MarkdownRender } from "./markdown-render.js";
|
|
5
|
+
export var MarkdownModule = ManaModule.create().register(MarkdownParser, MarkdownRender, LibroMarkdownSettingContribution);
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@difizen/libro-markdown",
|
|
3
|
+
"version": "0.0.0-snapshot-20241017072317",
|
|
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": "latest",
|
|
37
|
+
"@difizen/mana-l10n": "^0.1.24",
|
|
38
|
+
"@traptitech/markdown-it-katex": "^3.6.0",
|
|
39
|
+
"@types/markdown-it": "^12.2.3",
|
|
40
|
+
"katex": "^0.16.10",
|
|
41
|
+
"markdown-it": "^13.0.1",
|
|
42
|
+
"markdown-it-anchor": "^8.6.5"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"setup": "father build",
|
|
46
|
+
"build": "father build",
|
|
47
|
+
"test": ": Note: lint task is delegated to test:* scripts",
|
|
48
|
+
"test:vitest": "vitest run",
|
|
49
|
+
"test:jest": "jest",
|
|
50
|
+
"coverage": ": Note: lint task is delegated to coverage:* scripts",
|
|
51
|
+
"coverage:vitest": "vitest run --coverage",
|
|
52
|
+
"coverage:jest": "jest --coverage",
|
|
53
|
+
"lint": ": Note: lint task is delegated to lint:* scripts",
|
|
54
|
+
"lint:eslint": "eslint src",
|
|
55
|
+
"typecheck:tsc": "tsc --noEmit"
|
|
56
|
+
}
|
|
57
|
+
}
|
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
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ConfigurationContribution } from '@difizen/mana-app';
|
|
2
|
+
import { singleton } from '@difizen/mana-app';
|
|
3
|
+
|
|
4
|
+
import { LibroMarkdownConfiguration } from './config.js';
|
|
5
|
+
|
|
6
|
+
@singleton({ contrib: ConfigurationContribution })
|
|
7
|
+
export class LibroMarkdownSettingContribution implements ConfigurationContribution {
|
|
8
|
+
registerConfigurations() {
|
|
9
|
+
return [LibroMarkdownConfiguration.TargetToBlank];
|
|
10
|
+
}
|
|
11
|
+
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ConfigurationNode } from '@difizen/mana-app';
|
|
2
|
+
import { l10n } from '@difizen/mana-l10n';
|
|
3
|
+
|
|
4
|
+
export const LibroConfigLinkTargetToBlank: ConfigurationNode<boolean> = {
|
|
5
|
+
id: 'libro.markdown.link.target.to.blank',
|
|
6
|
+
description: l10n.t('markdown的a标签是否另起一页'),
|
|
7
|
+
title: l10n.t('a标签另起一页'),
|
|
8
|
+
type: 'checkbox',
|
|
9
|
+
defaultValue: true,
|
|
10
|
+
schema: {
|
|
11
|
+
type: 'boolean',
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const LibroMarkdownConfiguration = {
|
|
16
|
+
TargetToBlank: LibroConfigLinkTargetToBlank,
|
|
17
|
+
};
|
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,85 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ConfigurationService,
|
|
3
|
+
inject,
|
|
4
|
+
postConstruct,
|
|
5
|
+
singleton,
|
|
6
|
+
} from '@difizen/mana-app';
|
|
7
|
+
import latexPlugin from '@traptitech/markdown-it-katex';
|
|
8
|
+
import MarkdownIt from 'markdown-it';
|
|
9
|
+
|
|
10
|
+
import { libroAnchor, linkInsideHeader, slugify } from './anchor.js';
|
|
11
|
+
import { LibroMarkdownConfiguration } from './config.js';
|
|
12
|
+
import type { MarkdownRenderOption } from './markdown-protocol.js';
|
|
13
|
+
import { MarkdownParser } from './markdown-protocol.js';
|
|
14
|
+
import 'katex/dist/katex.min.css';
|
|
15
|
+
|
|
16
|
+
@singleton({ token: MarkdownParser })
|
|
17
|
+
export class MarkdownRender implements MarkdownParser {
|
|
18
|
+
protected mkt: MarkdownIt;
|
|
19
|
+
slugify = slugify;
|
|
20
|
+
enablePermalink = false;
|
|
21
|
+
@inject(ConfigurationService) protected configurationService: ConfigurationService;
|
|
22
|
+
|
|
23
|
+
@postConstruct()
|
|
24
|
+
init() {
|
|
25
|
+
this.mkt = new MarkdownIt({
|
|
26
|
+
html: true,
|
|
27
|
+
linkify: true,
|
|
28
|
+
});
|
|
29
|
+
this.mkt.linkify.set({ fuzzyLink: false });
|
|
30
|
+
this.mkt.use(libroAnchor, {
|
|
31
|
+
permalinkOptions: { class: 'libro-InternalAnchorLink', space: false },
|
|
32
|
+
permalink: this.enablePermalink ? linkInsideHeader : false,
|
|
33
|
+
slugify: this.slugify,
|
|
34
|
+
});
|
|
35
|
+
this.mkt.use(latexPlugin);
|
|
36
|
+
this.configurationService
|
|
37
|
+
.get(LibroMarkdownConfiguration.TargetToBlank)
|
|
38
|
+
.then((value) => {
|
|
39
|
+
if (value) {
|
|
40
|
+
this.mkt.use((md) => {
|
|
41
|
+
const defaultRender =
|
|
42
|
+
md.renderer.rules['link_open'] ||
|
|
43
|
+
function (tokens, idx, options, env, self) {
|
|
44
|
+
return self.renderToken(tokens, idx, options);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
md.renderer.rules['link_open'] = function (
|
|
48
|
+
tokens,
|
|
49
|
+
idx,
|
|
50
|
+
options,
|
|
51
|
+
env,
|
|
52
|
+
self,
|
|
53
|
+
) {
|
|
54
|
+
// 获取当前的token
|
|
55
|
+
const token = tokens[idx];
|
|
56
|
+
// 检查是否已有target属性
|
|
57
|
+
const targetIndex = token.attrIndex('target');
|
|
58
|
+
|
|
59
|
+
if (targetIndex < 0) {
|
|
60
|
+
// 如果没有target属性,添加target="_blank"
|
|
61
|
+
token.attrPush(['target', '_blank']);
|
|
62
|
+
} else {
|
|
63
|
+
// 如果已有target属性,修改为target="_blank"
|
|
64
|
+
if (token.attrs !== null) {
|
|
65
|
+
token.attrs[targetIndex][1] = '_blank';
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// 调用默认的渲染函数
|
|
70
|
+
return defaultRender(tokens, idx, options, env, self);
|
|
71
|
+
};
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
return;
|
|
75
|
+
})
|
|
76
|
+
.catch(() => {
|
|
77
|
+
//
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
render(markdownText: string, options?: MarkdownRenderOption): string {
|
|
82
|
+
const unsanitizedRenderedMarkdown = this.mkt.render(markdownText, options);
|
|
83
|
+
return unsanitizedRenderedMarkdown;
|
|
84
|
+
}
|
|
85
|
+
}
|
package/src/module.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ManaModule } from '@difizen/mana-app';
|
|
2
|
+
|
|
3
|
+
import { LibroMarkdownSettingContribution } from './config-contribution.js';
|
|
4
|
+
import { MarkdownParser } from './markdown-protocol.js';
|
|
5
|
+
import { MarkdownRender } from './markdown-render.js';
|
|
6
|
+
|
|
7
|
+
export const MarkdownModule = ManaModule.create().register(
|
|
8
|
+
MarkdownParser,
|
|
9
|
+
MarkdownRender,
|
|
10
|
+
LibroMarkdownSettingContribution,
|
|
11
|
+
);
|