@diplodoc/transform 4.54.0 → 4.57.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/dist/css/_yfm-only.css.map +1 -1
- package/dist/css/_yfm-only.min.css.map +1 -1
- package/dist/css/base.css +81 -14
- package/dist/css/base.css.map +3 -3
- package/dist/css/base.min.css +1 -1
- package/dist/css/base.min.css.map +3 -3
- package/dist/css/print.css.map +1 -1
- package/dist/css/yfm.css +81 -14
- package/dist/css/yfm.css.map +3 -3
- package/dist/css/yfm.min.css +1 -1
- package/dist/css/yfm.min.css.map +3 -3
- package/dist/js/_yfm-only.js.map +1 -1
- package/dist/js/_yfm-only.min.js.map +1 -1
- package/dist/js/base.js +47 -8
- package/dist/js/base.js.map +3 -3
- package/dist/js/base.min.js +1 -1
- package/dist/js/base.min.js.map +4 -4
- package/dist/js/yfm.js +47 -8
- package/dist/js/yfm.js.map +3 -3
- package/dist/js/yfm.min.js +1 -1
- package/dist/js/yfm.min.js.map +4 -4
- package/lib/constants.d.ts +2 -0
- package/lib/constants.js +5 -0
- package/lib/constants.js.map +1 -0
- package/lib/md.js +4 -2
- package/lib/md.js.map +1 -1
- package/lib/plugins/anchors/constants.d.ts +2 -0
- package/lib/plugins/anchors/constants.js +19 -1
- package/lib/plugins/anchors/constants.js.map +1 -1
- package/lib/plugins/anchors/index.d.ts +1 -0
- package/lib/plugins/anchors/index.js +22 -5
- package/lib/plugins/anchors/index.js.map +1 -1
- package/lib/plugins/code.js +26 -1
- package/lib/plugins/code.js.map +1 -1
- package/lib/plugins/notes/constants.d.ts +2 -1
- package/lib/plugins/notes/constants.js.map +1 -1
- package/lib/plugins/notes/index.js +1 -8
- package/lib/plugins/notes/index.js.map +1 -1
- package/lib/sanitize.js +30 -1
- package/lib/sanitize.js.map +1 -1
- package/lib/typings.d.ts +6 -1
- package/package.json +3 -3
- package/src/js/anchor.ts +18 -0
- package/src/js/base.ts +1 -0
- package/src/js/code.ts +14 -23
- package/src/js/utils.ts +20 -0
- package/src/scss/_anchor.scss +38 -8
- package/src/scss/_common.scss +22 -8
- package/src/transform/constants.ts +3 -0
- package/src/transform/md.ts +3 -0
- package/src/transform/plugins/anchors/constants.ts +21 -0
- package/src/transform/plugins/anchors/index.ts +52 -8
- package/src/transform/plugins/code.ts +34 -1
- package/src/transform/plugins/notes/constants.ts +3 -1
- package/src/transform/plugins/notes/index.ts +1 -10
- package/src/transform/sanitize.ts +33 -1
- package/src/transform/typings.ts +23 -1
package/lib/plugins/code.js
CHANGED
|
@@ -52,10 +52,35 @@ function termReplace(str, env, escape) {
|
|
|
52
52
|
const termCode = str.replace(reg, (_match, p1, _p2, p3) => `<i class="yfm yfm-term_title" term-key=":${p3}" id="${(0, utils_1.generateID)()}">${p1}</i>`);
|
|
53
53
|
return termCode || str;
|
|
54
54
|
}
|
|
55
|
+
function addLineNumbers(code) {
|
|
56
|
+
const lines = code.split('\n');
|
|
57
|
+
const lineCount = lines.length;
|
|
58
|
+
const maxDigits = String(lineCount).length;
|
|
59
|
+
// Remove trailing empty line if it exists
|
|
60
|
+
const hasTrailingNewline = code.endsWith('\n');
|
|
61
|
+
const linesToProcess = hasTrailingNewline ? lines.slice(0, -1) : lines;
|
|
62
|
+
return (linesToProcess
|
|
63
|
+
.map((line, index) => {
|
|
64
|
+
const lineNumber = String(index + 1).padStart(maxDigits, ' ');
|
|
65
|
+
return `<span class="yfm-line-number">${lineNumber}</span>${line}`;
|
|
66
|
+
})
|
|
67
|
+
.join('\n') + (hasTrailingNewline ? '\n' : ''));
|
|
68
|
+
}
|
|
55
69
|
const code = (md) => {
|
|
56
70
|
const superCodeRenderer = md.renderer.rules.fence;
|
|
57
71
|
md.renderer.rules.fence = function (tokens, idx, options, env, self) {
|
|
58
|
-
const
|
|
72
|
+
const token = tokens[idx];
|
|
73
|
+
const showLineNumbers = token.info.includes('showLineNumbers');
|
|
74
|
+
let superCode = superCodeRenderer === null || superCodeRenderer === void 0 ? void 0 : superCodeRenderer(tokens, idx, options, env, self);
|
|
75
|
+
if (superCode && showLineNumbers) {
|
|
76
|
+
// Extract the code content from the pre/code tags
|
|
77
|
+
const codeMatch = superCode.match(/<pre[^>]*><code[^>]*>([\s\S]*?)<\/code><\/pre>/);
|
|
78
|
+
if (codeMatch) {
|
|
79
|
+
const codeContent = codeMatch[1];
|
|
80
|
+
const codeWithLineNumbers = addLineNumbers(codeContent);
|
|
81
|
+
superCode = superCode.replace(codeContent, codeWithLineNumbers);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
59
84
|
const superCodeWithTerms = superCode && (env === null || env === void 0 ? void 0 : env.terms) ? termReplace(superCode, env, md.utils.escapeRE) : superCode;
|
|
60
85
|
return wrapInClipboard(superCodeWithTerms, idx);
|
|
61
86
|
};
|
package/lib/plugins/code.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"code.js","sourceRoot":"","sources":["../../src/transform/plugins/code.ts"],"names":[],"mappings":";AAAA,4BAA4B;AAG5B,mCAAmC;AAEnC,MAAM,eAAe,GAAG,CAAC,OAA2B,EAAE,EAAU,EAAE,EAAE;IAChE,OAAO;;UAED,OAAO;;yGAEwF,EAAE;;;;;;;;;;;;;gDAa3D,EAAE;;;;;;;;;4CASN,EAAE;;;;;mDAKK,EAAE;;;;;;;CAOpD,CAAC;AACF,CAAC,CAAC;AAQF,SAAS,WAAW,CAAC,GAAW,EAAE,GAAY,EAAE,MAA+B;IAC3E,MAAM,QAAQ,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC;SAClC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;SACzB,GAAG,CAAC,MAAM,CAAC;SACX,IAAI,CAAC,GAAG,CAAC,CAAC;IACf,MAAM,OAAO,GAAG,yBAAyB,GAAG,QAAQ,GAAG,OAAO,CAAC;IAC/D,MAAM,GAAG,GAAG,IAAI,MAAM,C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+
import { Lang } from '../typings';
|
|
2
|
+
export declare const TITLES: Record<Lang, Record<string, string>>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/transform/plugins/notes/constants.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/transform/plugins/notes/constants.ts"],"names":[],"mappings":";;;AAEa,QAAA,MAAM,GAAyC;IACxD,EAAE,EAAE;QACA,IAAI,EAAE,YAAY;QAClB,GAAG,EAAE,OAAO;QACZ,KAAK,EAAE,UAAU;QACjB,OAAO,EAAE,OAAO;KACnB;IACD,EAAE,EAAE;QACA,IAAI,EAAE,MAAM;QACZ,GAAG,EAAE,KAAK;QACV,KAAK,EAAE,OAAO;QACd,OAAO,EAAE,SAAS;KACrB;IACD,EAAE,EAAE;QACA,IAAI,EAAE,QAAQ;QACd,GAAG,EAAE,OAAO;QACZ,KAAK,EAAE,QAAQ;QACf,OAAO,EAAE,KAAK;KACjB;IACD,EAAE,EAAE;QACA,IAAI,EAAE,UAAU;QAChB,GAAG,EAAE,KAAK;QACV,KAAK,EAAE,YAAY;QACnB,OAAO,EAAE,UAAU;KACtB;IACD,EAAE,EAAE;QACA,IAAI,EAAE,UAAU;QAChB,GAAG,EAAE,QAAQ;QACb,KAAK,EAAE,QAAQ;QACf,OAAO,EAAE,eAAe;KAC3B;IACD,EAAE,EAAE;QACA,IAAI,EAAE,MAAM;QACZ,GAAG,EAAE,SAAS;QACd,KAAK,EAAE,QAAQ;QACf,OAAO,EAAE,OAAO;KACnB;IACD,EAAE,EAAE;QACA,IAAI,EAAE,MAAM;QACZ,GAAG,EAAE,KAAK;QACV,KAAK,EAAE,OAAO;QACd,OAAO,EAAE,OAAO;KACnB;IACD,EAAE,EAAE;QACA,IAAI,EAAE,WAAW;QACjB,GAAG,EAAE,OAAO;QACZ,KAAK,EAAE,UAAU;QACjB,OAAO,EAAE,OAAO;KACnB;IACD,EAAE,EAAE;QACA,IAAI,EAAE,QAAQ;QACd,GAAG,EAAE,SAAS;QACd,KAAK,EAAE,YAAY;QACnB,OAAO,EAAE,QAAQ;KACpB;IACD,EAAE,EAAE;QACA,IAAI,EAAE,UAAU;QAChB,GAAG,EAAE,UAAU;QACf,KAAK,EAAE,SAAS;QAChB,OAAO,EAAE,WAAW;KACvB;IACD,EAAE,EAAE;QACA,IAAI,EAAE,YAAY;QAClB,GAAG,EAAE,UAAU;QACf,KAAK,EAAE,SAAS;QAChB,OAAO,EAAE,YAAY;KACxB;IACD,EAAE,EAAE;QACA,IAAI,EAAE,IAAI;QACV,GAAG,EAAE,IAAI;QACT,KAAK,EAAE,IAAI;QACX,OAAO,EAAE,IAAI;KAChB;IACD,OAAO,EAAE;QACL,IAAI,EAAE,IAAI;QACV,GAAG,EAAE,IAAI;QACT,KAAK,EAAE,IAAI;QACX,OAAO,EAAE,IAAI;KAChB;IACD,EAAE,EAAE;QACA,IAAI,EAAE,SAAS;QACf,GAAG,EAAE,OAAO;QACZ,KAAK,EAAE,iBAAiB;QACxB,OAAO,EAAE,SAAS;KACrB;IACD,EAAE,EAAE;QACA,IAAI,EAAE,KAAK;QACX,GAAG,EAAE,OAAO;QACZ,KAAK,EAAE,QAAQ;QACf,OAAO,EAAE,QAAQ;KACpB;IACD,EAAE,EAAE;QACA,IAAI,EAAE,OAAO;QACb,GAAG,EAAE,UAAU;QACf,KAAK,EAAE,QAAQ;QACf,OAAO,EAAE,UAAU;KACtB;CACJ,CAAC"}
|
|
@@ -4,13 +4,6 @@ const utils_1 = require("../utils");
|
|
|
4
4
|
const constants_1 = require("./constants");
|
|
5
5
|
const ALERT_RE = /^{% note (alert|info|tip|warning)\s*(?:"(.*?)")? %}$/;
|
|
6
6
|
const WRONG_NOTES = /^{% note (.*)%}/;
|
|
7
|
-
function getTitle(type, originLang) {
|
|
8
|
-
let lang = originLang;
|
|
9
|
-
if (!lang || !Object.keys(constants_1.TITLES).includes(lang)) {
|
|
10
|
-
lang = 'ru';
|
|
11
|
-
}
|
|
12
|
-
return constants_1.TITLES[lang][type];
|
|
13
|
-
}
|
|
14
7
|
const matchCloseToken = (tokens, i) => {
|
|
15
8
|
return (tokens[i].type === 'paragraph_open' &&
|
|
16
9
|
tokens[i + 1].type === 'inline' &&
|
|
@@ -62,7 +55,7 @@ const index = (md, { lang, notesAutotitle, path: optPath, log }) => {
|
|
|
62
55
|
const titleClose = new state.Token('yfm_note_title_close', 'p', -1);
|
|
63
56
|
titleOpen.block = true;
|
|
64
57
|
titleClose.block = true;
|
|
65
|
-
const autotitle = notesAutotitle ?
|
|
58
|
+
const autotitle = notesAutotitle ? constants_1.TITLES[lang][type] : '';
|
|
66
59
|
titleInline.content = match[2] === undefined ? autotitle : match[2];
|
|
67
60
|
titleInline.children = [];
|
|
68
61
|
const contentOpen = new state.Token('yfm_note_content_open', 'div', 1);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/transform/plugins/notes/index.ts"],"names":[],"mappings":";AAAA,iCAA2B;AAK3B,oCAA6F;AAE7F,2CAAmC;AAEnC,MAAM,QAAQ,GAAG,sDAAsD,CAAC;AACxE,MAAM,WAAW,GAAG,iBAAiB,CAAC;AAEtC,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/transform/plugins/notes/index.ts"],"names":[],"mappings":";AAAA,iCAA2B;AAK3B,oCAA6F;AAE7F,2CAAmC;AAEnC,MAAM,QAAQ,GAAG,sDAAsD,CAAC;AACxE,MAAM,WAAW,GAAG,iBAAiB,CAAC;AAEtC,MAAM,eAAe,GAAuB,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;IACtD,OAAO,CACH,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,gBAAgB;QACnC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,KAAK,QAAQ;QAC/B,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,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199
200
|
const htmlAttrs = [
|
|
200
201
|
'accept',
|
|
@@ -509,6 +510,32 @@ const defaultCssWhitelist = Object.assign(Object.assign({}, cssfilter_1.default.
|
|
|
509
510
|
const yfmHtmlAttrs = ['note-type', 'term-key'];
|
|
510
511
|
const allowedTags = Array.from(new Set([...htmlTags, ...svgTags, ...sanitize_html_1.default.defaults.allowedTags]));
|
|
511
512
|
const allowedAttributes = Array.from(new Set([...htmlAttrs, ...svgAttrs, ...yfmHtmlAttrs]));
|
|
513
|
+
// For hrefs within "use" only allow local links to ids that start with "#"
|
|
514
|
+
const useTagTransformer = (tagName, attribs) => {
|
|
515
|
+
const cleanHref = (href) => {
|
|
516
|
+
if (href.startsWith('#')) {
|
|
517
|
+
return href;
|
|
518
|
+
}
|
|
519
|
+
else {
|
|
520
|
+
return null;
|
|
521
|
+
}
|
|
522
|
+
};
|
|
523
|
+
const cleanAttrs = (attrs) => {
|
|
524
|
+
const HREF_ATTRIBUTES = ['xlink:href', 'href'];
|
|
525
|
+
return Object.fromEntries(Object.entries(attrs)
|
|
526
|
+
.map(([key, value]) => {
|
|
527
|
+
if (HREF_ATTRIBUTES.includes(key)) {
|
|
528
|
+
return [key, cleanHref(value)];
|
|
529
|
+
}
|
|
530
|
+
return [key, value];
|
|
531
|
+
})
|
|
532
|
+
.filter(([_, value]) => value !== null));
|
|
533
|
+
};
|
|
534
|
+
return {
|
|
535
|
+
tagName,
|
|
536
|
+
attribs: cleanAttrs(attribs),
|
|
537
|
+
};
|
|
538
|
+
};
|
|
512
539
|
exports.defaultParseOptions = {
|
|
513
540
|
lowerCaseAttributeNames: false,
|
|
514
541
|
};
|
|
@@ -517,7 +544,9 @@ exports.defaultOptions = Object.assign(Object.assign({}, sanitize_html_1.default
|
|
|
517
544
|
'xlink:href',
|
|
518
545
|
'from',
|
|
519
546
|
'to',
|
|
520
|
-
], allowVulnerableTags: true, parser: exports.defaultParseOptions, cssWhiteList: defaultCssWhitelist
|
|
547
|
+
], allowVulnerableTags: true, parser: exports.defaultParseOptions, cssWhiteList: defaultCssWhitelist, transformTags: {
|
|
548
|
+
use: useTagTransformer,
|
|
549
|
+
} });
|
|
521
550
|
function sanitizeStyleTags(dom, cssWhiteList) {
|
|
522
551
|
const styleTags = dom('style');
|
|
523
552
|
styleTags.each((_index, element) => {
|
package/lib/sanitize.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sanitize.js","sourceRoot":"","sources":["../src/transform/sanitize.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"sanitize.js","sourceRoot":"","sources":["../src/transform/sanitize.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kEAA4D;AAC5D,aAAa;AACb,0DAAkC;AAClC,iDAAmC;AACnC,8CAAsB;AAGtB,gDAAwB;AAQxB,MAAM,QAAQ,GAAG;IACb,GAAG;IACH,MAAM;IACN,SAAS;IACT,SAAS;IACT,MAAM;IACN,SAAS;IACT,OAAO;IACP,OAAO;IACP,GAAG;IACH,KAAK;IACL,KAAK;IACL,KAAK;IACL,OAAO;IACP,YAAY;IACZ,MAAM;IACN,IAAI;IACJ,QAAQ;IACR,QAAQ;IACR,SAAS;IACT,QAAQ;IACR,MAAM;IACN,MAAM;IACN,KAAK;IACL,UAAU;IACV,SAAS;IACT,MAAM;IACN,UAAU;IACV,IAAI;IACJ,WAAW;IACX,KAAK;IACL,SAAS;IACT,KAAK;IACL,QAAQ;IACR,KAAK;IACL,KAAK;IACL,IAAI;IACJ,IAAI;IACJ,SAAS;IACT,IAAI;IACJ,UAAU;IACV,YAAY;IACZ,QAAQ;IACR,MAAM;IACN,QAAQ;IACR,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,MAAM;IACN,QAAQ;IACR,QAAQ;IACR,IAAI;IACJ,MAAM;IACN,GAAG;IACH,KAAK;IACL,O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C;AACvD,CAAC;AAnBD,4BAmBC;AAED,kBAAe,QAAQ,CAAC"}
|
package/lib/typings.d.ts
CHANGED
|
@@ -22,6 +22,7 @@ export declare type Heading = {
|
|
|
22
22
|
level: number;
|
|
23
23
|
items?: Heading[];
|
|
24
24
|
};
|
|
25
|
+
export declare type Lang = 'ru' | 'en' | 'ar' | 'cs' | 'fr' | 'es' | 'he' | 'bg' | 'et' | 'el' | 'pt' | 'zh' | 'zh-tw' | 'kk' | 'tr' | 'uz';
|
|
25
26
|
export interface OptionsType {
|
|
26
27
|
tokens?: boolean;
|
|
27
28
|
vars?: Record<string, string>;
|
|
@@ -58,6 +59,10 @@ export interface OptionsType {
|
|
|
58
59
|
* Set value to `false` to disable it
|
|
59
60
|
*/
|
|
60
61
|
enableMarkdownAttrs?: boolean;
|
|
62
|
+
supportGithubAnchors?: boolean;
|
|
63
|
+
disableCommonAnchors?: boolean;
|
|
64
|
+
useCommonAnchorButtons?: boolean;
|
|
65
|
+
lang?: Lang;
|
|
61
66
|
[x: string]: unknown;
|
|
62
67
|
}
|
|
63
68
|
export interface OutputType {
|
|
@@ -77,7 +82,7 @@ export declare type EnvType<Extras extends {} = {}> = {
|
|
|
77
82
|
export interface MarkdownItPluginOpts {
|
|
78
83
|
path: string;
|
|
79
84
|
log: Logger;
|
|
80
|
-
lang:
|
|
85
|
+
lang: Lang;
|
|
81
86
|
root: string;
|
|
82
87
|
rootPublicPath: string;
|
|
83
88
|
isLintRun: boolean;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@diplodoc/transform",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.57.0",
|
|
4
4
|
"description": "A simple transformer of text in YFM (Yandex Flavored Markdown) to HTML",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"markdown",
|
|
@@ -39,8 +39,8 @@
|
|
|
39
39
|
"artifact:pages": "npm run build && npm run artifact:playground",
|
|
40
40
|
"prepublishOnly": "npm run lint && npm run test && npm run build",
|
|
41
41
|
"test": "jest --coverage",
|
|
42
|
-
"test:playwright:nobuild": "cd e2e && npm run playwright:docker",
|
|
43
|
-
"test:playwright": "npm run build && npm run test:playwright:nobuild",
|
|
42
|
+
"test:playwright:nobuild": "cd e2e && npm run playwright:docker --",
|
|
43
|
+
"test:playwright": "npm run build && npm run test:playwright:nobuild --",
|
|
44
44
|
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
45
45
|
"lint": "lint update && lint",
|
|
46
46
|
"lint:fix": "lint update && lint fix",
|
package/src/js/anchor.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {copyToClipboard, getEventTarget, isCustom} from './utils';
|
|
2
|
+
|
|
3
|
+
const ANCHOR_BUTTON_SELECTOR = '.yfm-clipboard-anchor';
|
|
4
|
+
|
|
5
|
+
if (typeof document !== 'undefined') {
|
|
6
|
+
document.addEventListener('click', (event) => {
|
|
7
|
+
const target = getEventTarget(event) as HTMLElement;
|
|
8
|
+
|
|
9
|
+
if (isCustom(event) || !target.matches(ANCHOR_BUTTON_SELECTOR)) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const href = target.getAttribute('data-href') || '';
|
|
14
|
+
const link = new URL(href, window.location.href).toString();
|
|
15
|
+
|
|
16
|
+
copyToClipboard(link);
|
|
17
|
+
});
|
|
18
|
+
}
|
package/src/js/base.ts
CHANGED
package/src/js/code.ts
CHANGED
|
@@ -1,28 +1,7 @@
|
|
|
1
|
-
import {getEventTarget, isCustom} from './utils';
|
|
1
|
+
import {copyToClipboard, getEventTarget, isCustom} from './utils';
|
|
2
2
|
|
|
3
3
|
const BUTTON_SELECTOR = '.yfm-clipboard-button';
|
|
4
4
|
|
|
5
|
-
function copyToClipboard(text: string) {
|
|
6
|
-
if (!text) {
|
|
7
|
-
return Promise.resolve();
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
if (navigator.clipboard && typeof navigator.clipboard.writeText) {
|
|
11
|
-
return navigator.clipboard.writeText(text);
|
|
12
|
-
}
|
|
13
|
-
const textarea = document.createElement('textarea');
|
|
14
|
-
textarea.setAttribute('style', 'position: absolute; left: 1000%');
|
|
15
|
-
textarea.textContent = text;
|
|
16
|
-
document.body.append(textarea);
|
|
17
|
-
|
|
18
|
-
textarea.select();
|
|
19
|
-
document.execCommand('copy');
|
|
20
|
-
|
|
21
|
-
document.body.removeChild(textarea);
|
|
22
|
-
|
|
23
|
-
return Promise.resolve();
|
|
24
|
-
}
|
|
25
|
-
|
|
26
5
|
function notifySuccess(svgButton: HTMLElement | null) {
|
|
27
6
|
if (!svgButton) {
|
|
28
7
|
return;
|
|
@@ -58,7 +37,19 @@ if (typeof document !== 'undefined') {
|
|
|
58
37
|
return;
|
|
59
38
|
}
|
|
60
39
|
|
|
61
|
-
|
|
40
|
+
// Get all text nodes and filter out line numbers
|
|
41
|
+
const textContent = Array.from(code.childNodes)
|
|
42
|
+
.filter((node) => {
|
|
43
|
+
// Skip line number spans
|
|
44
|
+
if (node instanceof HTMLElement && node.classList.contains('yfm-line-number')) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
return true;
|
|
48
|
+
})
|
|
49
|
+
.map((node) => node.textContent)
|
|
50
|
+
.join('');
|
|
51
|
+
|
|
52
|
+
copyToClipboard(textContent).then(() => {
|
|
62
53
|
notifySuccess(parent.querySelector('.yfm-clipboard-icon'));
|
|
63
54
|
});
|
|
64
55
|
});
|
package/src/js/utils.ts
CHANGED
|
@@ -7,3 +7,23 @@ export const isCustom = (event: Event) => {
|
|
|
7
7
|
const target = getEventTarget(event);
|
|
8
8
|
return !target || !(target as HTMLElement).matches;
|
|
9
9
|
};
|
|
10
|
+
|
|
11
|
+
export const copyToClipboard = async (text: string) => {
|
|
12
|
+
if (!text) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (navigator.clipboard && typeof navigator.clipboard.writeText) {
|
|
17
|
+
return navigator.clipboard.writeText(text);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const textarea = document.createElement('textarea');
|
|
21
|
+
textarea.setAttribute('style', 'position: absolute; left: 1000%');
|
|
22
|
+
textarea.textContent = text;
|
|
23
|
+
document.body.append(textarea);
|
|
24
|
+
|
|
25
|
+
textarea.select();
|
|
26
|
+
document.execCommand('copy');
|
|
27
|
+
|
|
28
|
+
document.body.removeChild(textarea);
|
|
29
|
+
};
|
package/src/scss/_anchor.scss
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
.yfm {
|
|
2
|
-
h1,
|
|
3
|
-
|
|
2
|
+
h1,
|
|
3
|
+
h2,
|
|
4
|
+
h3,
|
|
5
|
+
h4,
|
|
6
|
+
h5,
|
|
7
|
+
h6 {
|
|
8
|
+
.yfm-clipboard-anchor {
|
|
9
|
+
//reset default button style
|
|
10
|
+
background: none;
|
|
11
|
+
color: var(--yfm-color-link);
|
|
12
|
+
border: none;
|
|
13
|
+
padding: 0;
|
|
14
|
+
font: inherit;
|
|
15
|
+
cursor: pointer;
|
|
16
|
+
|
|
17
|
+
&:hover {
|
|
18
|
+
color: var(--yfm-color-link-hover);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
.yfm-anchor,
|
|
23
|
+
.yfm-clipboard-anchor {
|
|
4
24
|
display: inline-block;
|
|
5
25
|
width: 24px;
|
|
6
26
|
padding-right: 4px;
|
|
@@ -22,23 +42,33 @@
|
|
|
22
42
|
}
|
|
23
43
|
}
|
|
24
44
|
|
|
25
|
-
&:hover
|
|
26
|
-
|
|
45
|
+
&:hover {
|
|
46
|
+
.yfm-anchor::before,
|
|
47
|
+
.yfm-clipboard-anchor::before {
|
|
48
|
+
opacity: 1;
|
|
49
|
+
}
|
|
27
50
|
}
|
|
28
51
|
}
|
|
29
52
|
|
|
30
53
|
table {
|
|
31
|
-
h1,
|
|
32
|
-
|
|
54
|
+
h1,
|
|
55
|
+
h2,
|
|
56
|
+
h3,
|
|
57
|
+
h4,
|
|
58
|
+
h5,
|
|
59
|
+
h6 {
|
|
60
|
+
.yfm-anchor,
|
|
61
|
+
.yfm-clipboard-anchor {
|
|
33
62
|
width: 1em;
|
|
34
63
|
margin-left: -1em;
|
|
35
64
|
padding-right: 0;
|
|
36
65
|
}
|
|
37
66
|
}
|
|
38
|
-
}
|
|
67
|
+
}
|
|
39
68
|
|
|
40
69
|
.yfm-tabs {
|
|
41
|
-
.yfm-anchor::before
|
|
70
|
+
.yfm-anchor::before,
|
|
71
|
+
.yfm-clipboard-anchor::before {
|
|
42
72
|
position: absolute;
|
|
43
73
|
padding-right: 2px;
|
|
44
74
|
}
|
package/src/scss/_common.scss
CHANGED
|
@@ -297,10 +297,10 @@
|
|
|
297
297
|
padding-left: 2.2em;
|
|
298
298
|
}
|
|
299
299
|
|
|
300
|
-
ul ul,
|
|
301
|
-
ul ol,
|
|
302
|
-
ol ol,
|
|
303
|
-
ol ul {
|
|
300
|
+
ul ul:last-child,
|
|
301
|
+
ul ol:last-child,
|
|
302
|
+
ol ol:last-child,
|
|
303
|
+
ol ul:last-child {
|
|
304
304
|
margin-top: 0;
|
|
305
305
|
margin-bottom: 0;
|
|
306
306
|
}
|
|
@@ -309,6 +309,7 @@
|
|
|
309
309
|
word-wrap: break-word;
|
|
310
310
|
}
|
|
311
311
|
|
|
312
|
+
li:first-child,
|
|
312
313
|
li + li {
|
|
313
314
|
margin-block: var(--yfm-list-item-margin-block, 0.33em 0);
|
|
314
315
|
}
|
|
@@ -319,14 +320,16 @@
|
|
|
319
320
|
margin-block: var(--yfm-list-text-margin-block, 0 15px);
|
|
320
321
|
}
|
|
321
322
|
|
|
322
|
-
> p:only-
|
|
323
|
-
> blockquote:only-
|
|
323
|
+
> p:only-of-type,
|
|
324
|
+
> blockquote:only-of-type,
|
|
325
|
+
> p:first-child:has(+ ul),
|
|
326
|
+
> p:first-child:has(+ ol) {
|
|
324
327
|
margin-block: var(--yfm-list-text-only-margin-block, 0);
|
|
325
328
|
}
|
|
326
329
|
|
|
327
330
|
@supports selector(:has(br)) {
|
|
328
|
-
> p:only-
|
|
329
|
-
> blockquote:only-
|
|
331
|
+
> p:only-of-type:has(br),
|
|
332
|
+
> blockquote:only-of-type:has(br) {
|
|
330
333
|
margin-block: var(--yfm-list-text-margin-block, 0 15px);
|
|
331
334
|
}
|
|
332
335
|
}
|
|
@@ -342,6 +345,10 @@
|
|
|
342
345
|
tab-size: var(--yfm-tab-size-code, inherit);
|
|
343
346
|
}
|
|
344
347
|
|
|
348
|
+
a code {
|
|
349
|
+
color: inherit;
|
|
350
|
+
}
|
|
351
|
+
|
|
345
352
|
pre {
|
|
346
353
|
word-wrap: normal;
|
|
347
354
|
}
|
|
@@ -370,6 +377,13 @@
|
|
|
370
377
|
color: var(--yfm-color-text);
|
|
371
378
|
|
|
372
379
|
white-space: pre;
|
|
380
|
+
|
|
381
|
+
.yfm-line-number {
|
|
382
|
+
display: inline-block;
|
|
383
|
+
padding-right: 1em;
|
|
384
|
+
color: var(--yfm-color-line-numbers, #666);
|
|
385
|
+
user-select: none;
|
|
386
|
+
}
|
|
373
387
|
}
|
|
374
388
|
|
|
375
389
|
sup,
|
package/src/transform/md.ts
CHANGED
|
@@ -12,6 +12,7 @@ import extractTitle from './title';
|
|
|
12
12
|
import getHeadings from './headings';
|
|
13
13
|
import sanitizeHtml, {defaultOptions, sanitizeStyles} from './sanitize';
|
|
14
14
|
import {olAttrConversion} from './plugins/ol-attr-conversion';
|
|
15
|
+
import {DEFAULT_LANG} from './constants';
|
|
15
16
|
|
|
16
17
|
function initMarkdownIt(options: OptionsType) {
|
|
17
18
|
const {
|
|
@@ -74,6 +75,7 @@ function getPluginOptions(options: OptionsType) {
|
|
|
74
75
|
extractTitle,
|
|
75
76
|
conditionsInCode = false,
|
|
76
77
|
disableLiquid = false,
|
|
78
|
+
lang = DEFAULT_LANG,
|
|
77
79
|
...customOptions
|
|
78
80
|
} = options;
|
|
79
81
|
|
|
@@ -85,6 +87,7 @@ function getPluginOptions(options: OptionsType) {
|
|
|
85
87
|
extractTitle,
|
|
86
88
|
disableLiquid,
|
|
87
89
|
log,
|
|
90
|
+
lang,
|
|
88
91
|
} as MarkdownItPluginOpts;
|
|
89
92
|
}
|
|
90
93
|
|
|
@@ -1,2 +1,23 @@
|
|
|
1
|
+
import {Lang} from '../typings';
|
|
2
|
+
|
|
1
3
|
export const CUSTOM_ID_REGEXP = /\[?{ ?#(\S+) ?}]?/g;
|
|
2
4
|
export const CUSTOM_ID_EXCEPTION = '[{#T}]';
|
|
5
|
+
|
|
6
|
+
export const ANCHOR_TITLES: Record<Lang, string> = {
|
|
7
|
+
ru: 'Скопировать ссылку',
|
|
8
|
+
en: 'Copy link',
|
|
9
|
+
ar: 'انسخ الرابط',
|
|
10
|
+
cs: 'Zkopírovat odkaz',
|
|
11
|
+
fr: 'Copier le lien',
|
|
12
|
+
es: 'Copiar enlace',
|
|
13
|
+
he: 'העתק קישור',
|
|
14
|
+
bg: 'Копиране на връзката',
|
|
15
|
+
et: 'Kopeeri viide',
|
|
16
|
+
el: 'Αντιγράψτε τον σύνδεσμο',
|
|
17
|
+
pt: 'Copiar o link',
|
|
18
|
+
zh: '复制链接',
|
|
19
|
+
'zh-tw': '複製連結',
|
|
20
|
+
kk: 'Сілтемені көшіру',
|
|
21
|
+
tr: 'Bağlantıyı kopyala',
|
|
22
|
+
uz: 'Havolani nusxalash',
|
|
23
|
+
};
|
|
@@ -8,14 +8,36 @@ import slugify from 'slugify';
|
|
|
8
8
|
import {headingInfo} from '../../utils';
|
|
9
9
|
import {MarkdownItPluginCb} from '../typings';
|
|
10
10
|
|
|
11
|
-
import {CUSTOM_ID_EXCEPTION, CUSTOM_ID_REGEXP} from './constants';
|
|
11
|
+
import {ANCHOR_TITLES, CUSTOM_ID_EXCEPTION, CUSTOM_ID_REGEXP} from './constants';
|
|
12
12
|
|
|
13
|
-
function
|
|
13
|
+
function createAnchorButtonTokens(
|
|
14
14
|
state: StateCore,
|
|
15
15
|
id: string,
|
|
16
|
+
setId = false,
|
|
17
|
+
href: string,
|
|
16
18
|
title: string,
|
|
19
|
+
) {
|
|
20
|
+
const open = new state.Token('anchor_open', 'button', 1);
|
|
21
|
+
const close = new state.Token('anchor_close', 'button', -1);
|
|
22
|
+
|
|
23
|
+
if (setId) {
|
|
24
|
+
open.attrSet('id', id);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
open.attrSet('class', 'yfm-clipboard-anchor');
|
|
28
|
+
open.attrSet('data-href', href + '#' + id);
|
|
29
|
+
open.attrSet('aria-label', title);
|
|
30
|
+
open.attrSet('title', title);
|
|
31
|
+
|
|
32
|
+
return [open, close];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function createAnchorLinkTokens(
|
|
36
|
+
state: StateCore,
|
|
37
|
+
id: string,
|
|
17
38
|
setId = false,
|
|
18
39
|
href: string,
|
|
40
|
+
title: string,
|
|
19
41
|
) {
|
|
20
42
|
const open = new state.Token('link_open', 'a', 1);
|
|
21
43
|
const close = new state.Token('link_close', 'a', -1);
|
|
@@ -76,13 +98,22 @@ interface Options {
|
|
|
76
98
|
extractTitle?: boolean;
|
|
77
99
|
supportGithubAnchors?: boolean;
|
|
78
100
|
disableCommonAnchors?: boolean;
|
|
101
|
+
useCommonAnchorButtons?: boolean;
|
|
79
102
|
transformLink: (v: string) => string;
|
|
80
103
|
getPublicPath?: (options: Options, v?: string) => string;
|
|
81
104
|
}
|
|
82
105
|
|
|
83
106
|
const index: MarkdownItPluginCb<Options> = (md, options) => {
|
|
84
|
-
const {
|
|
85
|
-
|
|
107
|
+
const {
|
|
108
|
+
extractTitle,
|
|
109
|
+
path,
|
|
110
|
+
log,
|
|
111
|
+
supportGithubAnchors,
|
|
112
|
+
getPublicPath,
|
|
113
|
+
disableCommonAnchors,
|
|
114
|
+
useCommonAnchorButtons,
|
|
115
|
+
lang,
|
|
116
|
+
} = options;
|
|
86
117
|
|
|
87
118
|
const plugin = (state: StateCore) => {
|
|
88
119
|
/* Do not use the plugin if it is included in the file */
|
|
@@ -97,6 +128,10 @@ const index: MarkdownItPluginCb<Options> = (md, options) => {
|
|
|
97
128
|
let i = 0;
|
|
98
129
|
const slugger = new GithubSlugger();
|
|
99
130
|
|
|
131
|
+
const createAnchorTokens = useCommonAnchorButtons
|
|
132
|
+
? createAnchorButtonTokens
|
|
133
|
+
: createAnchorLinkTokens;
|
|
134
|
+
|
|
100
135
|
while (i < tokens.length) {
|
|
101
136
|
const token = tokens[i];
|
|
102
137
|
const isHeading = token.type === 'heading_open';
|
|
@@ -141,22 +176,31 @@ const index: MarkdownItPluginCb<Options> = (md, options) => {
|
|
|
141
176
|
}
|
|
142
177
|
|
|
143
178
|
const allAnchorIds = customIds ? customIds : [id];
|
|
144
|
-
const anchorTitle =
|
|
179
|
+
const anchorTitle = useCommonAnchorButtons
|
|
180
|
+
? ANCHOR_TITLES[lang]
|
|
181
|
+
: removeCustomId(title).replace(/`/g, '');
|
|
182
|
+
|
|
145
183
|
allAnchorIds.forEach((customId) => {
|
|
146
184
|
const setId = id !== customId;
|
|
147
185
|
if (!disableCommonAnchors) {
|
|
148
|
-
const linkTokens =
|
|
186
|
+
const linkTokens = createAnchorTokens(
|
|
149
187
|
state,
|
|
150
188
|
customId,
|
|
151
|
-
anchorTitle,
|
|
152
189
|
setId,
|
|
153
190
|
href,
|
|
191
|
+
anchorTitle,
|
|
154
192
|
);
|
|
155
193
|
inlineToken.children?.unshift(...linkTokens);
|
|
156
194
|
}
|
|
157
195
|
|
|
158
196
|
if (supportGithubAnchors) {
|
|
159
|
-
const ghLinkTokens =
|
|
197
|
+
const ghLinkTokens = createAnchorTokens(
|
|
198
|
+
state,
|
|
199
|
+
ghId,
|
|
200
|
+
true,
|
|
201
|
+
href,
|
|
202
|
+
anchorTitle,
|
|
203
|
+
);
|
|
160
204
|
inlineToken.children?.unshift(...ghLinkTokens);
|
|
161
205
|
}
|
|
162
206
|
});
|