expressive-code-links 1.1.0 → 1.2.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/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -51,8 +51,9 @@ var LinkAnnotation = class extends ExpressiveCodeAnnotation {
|
|
|
51
51
|
render({ nodesToTransform }) {
|
|
52
52
|
return nodesToTransform.map((node) => {
|
|
53
53
|
if (node.type !== "element") {
|
|
54
|
+
const getText = (el) => el.value ?? el.children?.map(getText).join("") ?? "";
|
|
54
55
|
throw new Error(
|
|
55
|
-
"[expressive-code-links]: Used link annotation spanning multiple expressive-code elements
|
|
56
|
+
"[expressive-code-links]: Used link annotation spanning multiple expressive-code elements:\n " + getText(node)
|
|
56
57
|
);
|
|
57
58
|
}
|
|
58
59
|
return h(
|
package/dist/index.js.map
CHANGED
|
@@ -1 +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 if (node.type !== 'element') {\n // e.g.\n // con[st x](href) = 1\n // \n // If EC considers `const` as a separate `span` element from `x`,\n // which is needs to do to give them different colors,\n // then this bit of code will break.\n //\n // I'm not aware of any other applicable scenarios.\n\n throw new Error(\n '[expressive-code-links]: Used link annotation spanning multiple expressive-code elements
|
|
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 if (node.type !== 'element') {\n // e.g.\n // con[st x](href) = 1\n // \n // If EC considers `const` as a separate `span` element from `x`,\n // which is needs to do to give them different colors,\n // then this bit of code will break.\n //\n // I'm not aware of any other applicable scenarios.\n\n type Node = object & { value?: string, children?: Node[] };\n\n const getText = (el: Node): string =>\n el.value ?? el.children?.map(getText).join('') ?? '';\n\n throw new Error(\n '[expressive-code-links]: Used link annotation spanning multiple expressive-code elements:\\n'\n + ' ' + getText(node)\n );\n }\n\n return h(\n node.tagName, \n node.properties,\n h(\n 'a.ec-link',\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,C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