expressive-code-links 1.0.0 → 1.0.1

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.
@@ -0,0 +1,5 @@
1
+ import * as _expressive_code_core from '@expressive-code/core';
2
+
3
+ declare function pluginLink(): _expressive_code_core.ExpressiveCodePlugin;
4
+
5
+ export { pluginLink };
package/dist/index.js ADDED
@@ -0,0 +1,73 @@
1
+ // src/index.ts
2
+ import { definePlugin, ExpressiveCodeAnnotation } from "@expressive-code/core";
3
+ import { h } from "@expressive-code/core/hast";
4
+ function getRegex() {
5
+ const capture = (r) => "(" + r + ")";
6
+ const escape = (c) => "\\" + c;
7
+ const allExcept = (c) => "[^" + c + "]";
8
+ const oneOrMore = (r) => r + "+";
9
+ const surrounded = (start, wrapper, end) => start + wrapper(oneOrMore(allExcept(end))) + end;
10
+ const link = surrounded(escape("["), capture, escape("]"));
11
+ const href = surrounded(escape("("), capture, escape(")"));
12
+ const regex = escape("\\") + link + href;
13
+ return new RegExp(regex, "g");
14
+ }
15
+ function pluginLink() {
16
+ return definePlugin({
17
+ name: "Links inside code blocks, e.g. \\[link][href]",
18
+ hooks: {
19
+ preprocessCode: (context) => {
20
+ for (const line of context.codeBlock.getLines()) {
21
+ const matches = [...line.text.matchAll(getRegex())];
22
+ let offset = 0;
23
+ for (const match of matches) {
24
+ const [original, link, href] = match;
25
+ const from = (match.index || 0) - offset;
26
+ const to = from + original.length;
27
+ line.addAnnotation(
28
+ new LinkAnnotation({
29
+ href,
30
+ inlineRange: {
31
+ columnStart: from,
32
+ columnEnd: to
33
+ }
34
+ })
35
+ );
36
+ line.editText(from, to, link);
37
+ offset += original.length - link.length;
38
+ }
39
+ }
40
+ }
41
+ }
42
+ });
43
+ }
44
+ var LinkAnnotation = class extends ExpressiveCodeAnnotation {
45
+ href;
46
+ constructor(options) {
47
+ const { href, ...original } = options;
48
+ super(original);
49
+ this.href = href;
50
+ }
51
+ render({ nodesToTransform }) {
52
+ return nodesToTransform.map((node) => {
53
+ if (node.type !== "element")
54
+ throw new Error("not an element: " + JSON.stringify(node));
55
+ return h(
56
+ node.tagName,
57
+ node.properties,
58
+ h(
59
+ "a",
60
+ {
61
+ href: this.href,
62
+ style: "color: inherit"
63
+ },
64
+ ...node.children
65
+ )
66
+ );
67
+ });
68
+ }
69
+ };
70
+ export {
71
+ pluginLink
72
+ };
73
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { definePlugin, ExpressiveCodeAnnotation, type AnnotationRenderOptions } from '@expressive-code/core'\nimport { h } from '@expressive-code/core/hast';\n\nfunction getRegex() {\n // ends up looking something like this: \n //\n // \\ [link ] (href )\n // /\\\\(\\[(.+)\\]\\((.+)\\))/g\n\n const capture = (r: string) => '(' + r + ')';\n const escape = (c: string) => '\\\\' + c;\n const allExcept = (c: string) => '[^' + c + ']';\n const oneOrMore = (r: string) => r + '+';\n\n const surrounded = (start: string, wrapper: (x: string) => string, end: string) =>\n start + wrapper(oneOrMore(allExcept(end))) + end;\n\n const link = surrounded(escape('['), capture, escape(']'))\n const href = surrounded(escape('('), capture, escape(')'))\n\n // currently doesn't allow escaping the escape\n const regex = escape('\\\\') + link + href;\n\n return new RegExp(regex, 'g');\n}\n\nexport function pluginLink() {\n return definePlugin({\n name: 'Links inside code blocks, e.g. \\\\[link][href]',\n hooks: {\n preprocessCode: (context) => {\n for (const line of context.codeBlock.getLines()) {\n const matches = [...line.text.matchAll(getRegex())];\n\n // account for multiple links in one line\n let offset = 0;\n\n for (const match of matches) {\n const [original, link, href] = match;\n\n const from = (match.index || 0) - offset;\n const to = from + original.length;\n\n line.addAnnotation(\n new LinkAnnotation({\n href,\n inlineRange: {\n columnStart: from,\n columnEnd: to,\n }\n })\n )\n // this already culls annotation\n line.editText(from, to, link);\n\n offset += original.length - link.length;\n }\n }\n }\n },\n })\n}\n\nclass LinkAnnotation extends ExpressiveCodeAnnotation {\n href: string;\n constructor(options: ConstructorParameters<typeof ExpressiveCodeAnnotation>[0] & { href: string }) {\n const { href, ...original } = options;\n super(original);\n\n this.href = href;\n }\n render({ nodesToTransform }: AnnotationRenderOptions) {\n return nodesToTransform.map((node) => {\n // appease ts\n if (node.type !== 'element') throw new Error('not an element: ' + JSON.stringify(node));\n\n return h(\n node.tagName, \n node.properties,\n h(\n 'a',\n {\n href: this.href,\n style: 'color: inherit',\n },\n ...node.children\n )\n )\n })\n }\n}\n\n"],"mappings":";AAAA,SAAS,cAAc,gCAA8D;AACrF,SAAS,SAAS;AAElB,SAAS,WAAW;AAMlB,QAAM,UAAU,CAAC,MAAc,MAAM,IAAI;AACzC,QAAM,SAAS,CAAC,MAAc,OAAO;AACrC,QAAM,YAAY,CAAC,MAAc,OAAO,IAAI;AAC5C,QAAM,YAAY,CAAC,MAAc,IAAI;AAErC,QAAM,aAAa,CAAC,OAAe,SAAgC,QACjE,QAAQ,QAAQ,UAAU,UAAU,GAAG,CAAC,CAAC,IAAI;AAE/C,QAAM,OAAO,WAAW,OAAO,GAAG,GAAG,SAAS,OAAO,GAAG,CAAC;AACzD,QAAM,OAAO,WAAW,OAAO,GAAG,GAAG,SAAS,OAAO,GAAG,CAAC;AAGzD,QAAM,QAAQ,OAAO,IAAI,IAAI,OAAO;AAEpC,SAAO,IAAI,OAAO,OAAO,GAAG;AAC9B;AAEO,SAAS,aAAa;AAC3B,SAAO,aAAa;AAAA,IAClB,MAAM;AAAA,IACN,OAAO;AAAA,MACL,gBAAgB,CAAC,YAAY;AAC3B,mBAAW,QAAQ,QAAQ,UAAU,SAAS,GAAG;AAC/C,gBAAM,UAAU,CAAC,GAAG,KAAK,KAAK,SAAS,SAAS,CAAC,CAAC;AAGlD,cAAI,SAAS;AAEb,qBAAW,SAAS,SAAS;AAC3B,kBAAM,CAAC,UAAU,MAAM,IAAI,IAAI;AAE/B,kBAAM,QAAQ,MAAM,SAAS,KAAK;AAClC,kBAAM,KAAK,OAAO,SAAS;AAE3B,iBAAK;AAAA,cACH,IAAI,eAAe;AAAA,gBACjB;AAAA,gBACA,aAAa;AAAA,kBACX,aAAa;AAAA,kBACb,WAAW;AAAA,gBACb;AAAA,cACF,CAAC;AAAA,YACH;AAEA,iBAAK,SAAS,MAAM,IAAI,IAAI;AAE5B,sBAAU,SAAS,SAAS,KAAK;AAAA,UACnC;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF,CAAC;AACH;AAEA,IAAM,iBAAN,cAA6B,yBAAyB;AAAA,EACpD;AAAA,EACA,YAAY,SAAuF;AACjG,UAAM,EAAE,MAAM,GAAG,SAAS,IAAI;AAC9B,UAAM,QAAQ;AAEd,SAAK,OAAO;AAAA,EACd;AAAA,EACA,OAAO,EAAE,iBAAiB,GAA4B;AACpD,WAAO,iBAAiB,IAAI,CAAC,SAAS;AAEpC,UAAI,KAAK,SAAS;AAAW,cAAM,IAAI,MAAM,qBAAqB,KAAK,UAAU,IAAI,CAAC;AAEtF,aAAO;AAAA,QACL,KAAK;AAAA,QACL,KAAK;AAAA,QACL;AAAA,UACE;AAAA,UACA;AAAA,YACE,MAAM,KAAK;AAAA,YACX,OAAO;AAAA,UACT;AAAA,UACA,GAAG,KAAK;AAAA,QACV;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH;AACF;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expressive-code-links",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Links inside code blocks, by escaping markdown syntax",
5
5
  "keywords": [],
6
6
  "homepage": "https://github.com/towc/expressive-code-links#readme",