Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Socket
Sign inDemoInstall

remark-shiki-twoslash

Package Overview
Dependencies
Maintainers
1
Versions
51
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

remark-shiki-twoslash - npm Package Compare versions

Comparing version 1.2.0 to 1.2.1

9

dist/index.d.ts
import { UserConfigSettings } from "shiki-twoslash";
import type { Lang, Highlighter } from "shiki";
import { Lang, Highlighter } from "shiki";
import { Node } from "unist";

@@ -22,3 +22,10 @@ declare type RichNode = Node & {

export declare const runTwoSlashOnNode: (settings?: UserConfigSettings) => (node: RichNode) => void;
/**
* Keeps a cache of the JSON responses to a twoslash call in node_modules/.cache/twoslash
* which should keep CI times down (e.g. the epub vs the handbook etc) - but also during
* dev time, this is useful.
*
*/
export declare const cachedTwoslashCall: (code: string, lang: string, settings: UserConfigSettings) => any;
declare function remarkTwoslash(settings?: UserConfigSettings): (markdownAST: any) => Promise<void>;
export default remarkTwoslash;

@@ -8,2 +8,3 @@ 'use strict';

var shikiTwoslash = require('shiki-twoslash');
var shiki = require('shiki');
var visit = _interopDefault(require('unist-util-visit'));

@@ -854,5 +855,6 @@

var lang = node.lang;
var settings = twoslashSettings || {};
var shouldDisableTwoslash = process && process.env && !!process.env.TWOSLASH_DISABLE; // Run twoslash
var settings = twoslashSettings || {}; // Offer a way to do high-perf iterations, this is less useful
// given that we cache the results of twoslash in the file-system
var shouldDisableTwoslash = process && process.env && !!process.env.TWOSLASH_DISABLE;
if (!shouldDisableTwoslash) runTwoSlashOnNode(settings)(node); // Shiki doesn't respect json5 as an input, so switch it

@@ -865,5 +867,6 @@ // to json, which can handle comments in the syntax highlight

if (replacer[lang]) lang = replacer[lang];
if (replacer[lang]) lang = replacer[lang]; // The meta is the bit after lang in: ```lang [this bit]
var metaString = !node.meta ? "" : typeof node.meta === "string" ? node.meta : node.meta.join(" ");
var results;
var results; // Support 'twoslash' codesamples

@@ -875,2 +878,3 @@ if (lang === "twoslash") {

} else {
// All good, get each
var output = highlighters.map(function (highlighter) {

@@ -903,11 +907,63 @@ // @ts-ignore

if (node.meta && node.meta.includes("twoslash")) {
var code = replaceIncludesInCode(includes, node.value);
var results = shikiTwoslash.runTwoSlash(code, node.lang, settings);
node.value = results.code;
node.lang = results.extension;
node.twoslash = results;
try {
var code = replaceIncludesInCode(includes, node.value);
var results = cachedTwoslashCall(code, node.lang, settings);
node.value = results.code;
node.lang = results.extension;
node.twoslash = results;
} catch (error) {
var pos = node.position && node.position.start.line || -1;
error.message = "remark-shiki-twoslash: Error thrown in code sample on line " + pos + "\n\n" + error.message;
throw error;
}
}
};
};
/**
* Keeps a cache of the JSON responses to a twoslash call in node_modules/.cache/twoslash
* which should keep CI times down (e.g. the epub vs the handbook etc) - but also during
* dev time, this is useful.
*
*/
var cachedTwoslashCall = function cachedTwoslashCall(code, lang, settings) {
try {
require("crypto");
} catch (err) {
// Not in Node, run un-cached
return shikiTwoslash.runTwoSlash(code, lang, settings);
}
var _require = require("crypto"),
createHash = _require.createHash;
var _require2 = require("fs"),
readFileSync = _require2.readFileSync,
existsSync = _require2.existsSync,
mkdirSync = _require2.mkdirSync,
writeFileSync = _require2.writeFileSync;
var _require3 = require("path"),
join = _require3.join;
var shasum = createHash("sha1");
var codeSha = shasum.update(code).digest("hex");
var cacheRoot = join(__dirname, "..", "..", ".cache", "twoslash");
var cachePath = join(cacheRoot, codeSha + ".json");
if (existsSync(cachePath)) {
return JSON.parse(readFileSync(cachePath, "utf8"));
} else {
var results = shikiTwoslash.runTwoSlash(code, lang, settings);
if (!existsSync(cacheRoot)) mkdirSync(cacheRoot, {
recursive: true
});
writeFileSync(cachePath, JSON.stringify(results), "utf8");
return results;
}
}; // The remark API
// So we only have one highlighter per theme in a process
var highlighterCache = /*#__PURE__*/new Map();
function remarkTwoslash(settings) {

@@ -939,3 +995,3 @@ if (settings === void 0) {

var _ref2 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(theme) {
var highlighter;
var themeName, cached, highlighter;
return runtime_1.wrap(function _callee$(_context) {

@@ -945,4 +1001,16 @@ while (1) {

case 0:
_context.next = 2;
return shikiTwoslash.createShikiHighlighter(_extends({}, settings, {
// You can put a string, a path, or the JSON theme obj
themeName = theme.name || theme;
cached = highlighterCache.get(themeName);
if (!cached) {
_context.next = 4;
break;
}
return _context.abrupt("return", cached);
case 4:
_context.next = 6;
return shiki.getHighlighter(_extends({}, settings, {
theme: theme,

@@ -952,9 +1020,10 @@ themes: undefined

case 2:
case 6:
highlighter = _context.sent;
// @ts-ignore - https://github.com/shikijs/shiki/pull/162 will fix this
highlighter.customName = theme;
highlighter.customName = themeName;
highlighterCache.set(themeName, highlighter);
return _context.abrupt("return", highlighter);
case 5:
case 10:
case "end":

@@ -993,2 +1062,3 @@ return _context.stop();

exports.cachedTwoslashCall = cachedTwoslashCall;
exports.default = remarkTwoslash;

@@ -995,0 +1065,0 @@ exports.runTwoSlashOnNode = runTwoSlashOnNode;

2

dist/remark-shiki-twoslash.cjs.production.min.js

@@ -1,2 +0,2 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,r=require("shiki-twoslash"),e=(t=require("unist-util-visit"))&&"object"==typeof t&&"default"in t?t.default:t;function n(t,r,e,n,o,i,a){try{var c=t[i](a),u=c.value}catch(t){return void e(t)}c.done?r(u):Promise.resolve(u).then(n,o)}function o(t){return function(){var r=this,e=arguments;return new Promise((function(o,i){var a=t.apply(r,e);function c(t){n(a,o,i,c,u,"next",t)}function u(t){n(a,o,i,c,u,"throw",t)}c(void 0)}))}}function i(){return(i=Object.assign||function(t){for(var r=1;r<arguments.length;r++){var e=arguments[r];for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])}return t}).apply(this,arguments)}function a(t,r){return t(r={exports:{}},r.exports),r.exports}var c=a((function(t){var r=function(t){var r,e=Object.prototype,n=e.hasOwnProperty,o="function"==typeof Symbol?Symbol:{},i=o.iterator||"@@iterator",a=o.asyncIterator||"@@asyncIterator",c=o.toStringTag||"@@toStringTag";function u(t,r,e){return Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{u({},"")}catch(t){u=function(t,r,e){return t[r]=e}}function s(t,r,e,n){var o=Object.create((r&&r.prototype instanceof d?r:d).prototype),i=new S(n||[]);return o._invoke=function(t,r,e){var n=l;return function(o,i){if(n===p)throw new Error("Generator is already running");if(n===v){if("throw"===o)throw i;return P()}for(e.method=o,e.arg=i;;){var a=e.delegate;if(a){var c=_(a,e);if(c){if(c===y)continue;return c}}if("next"===e.method)e.sent=e._sent=e.arg;else if("throw"===e.method){if(n===l)throw n=v,e.arg;e.dispatchException(e.arg)}else"return"===e.method&&e.abrupt("return",e.arg);n=p;var u=h(t,r,e);if("normal"===u.type){if(n=e.done?v:f,u.arg===y)continue;return{value:u.arg,done:e.done}}"throw"===u.type&&(n=v,e.method="throw",e.arg=u.arg)}}}(t,e,i),o}function h(t,r,e){try{return{type:"normal",arg:t.call(r,e)}}catch(t){return{type:"throw",arg:t}}}t.wrap=s;var l="suspendedStart",f="suspendedYield",p="executing",v="completed",y={};function d(){}function m(){}function g(){}var w={};w[i]=function(){return this};var x=Object.getPrototypeOf,L=x&&x(x(N([])));L&&L!==e&&n.call(L,i)&&(w=L);var b=g.prototype=d.prototype=Object.create(w);function E(t){["next","throw","return"].forEach((function(r){u(t,r,(function(t){return this._invoke(r,t)}))}))}function j(t,r){var e;this._invoke=function(o,i){function a(){return new r((function(e,a){!function e(o,i,a,c){var u=h(t[o],t,i);if("throw"!==u.type){var s=u.arg,l=s.value;return l&&"object"==typeof l&&n.call(l,"__await")?r.resolve(l.__await).then((function(t){e("next",t,a,c)}),(function(t){e("throw",t,a,c)})):r.resolve(l).then((function(t){s.value=t,a(s)}),(function(t){return e("throw",t,a,c)}))}c(u.arg)}(o,i,e,a)}))}return e=e?e.then(a,a):a()}}function _(t,e){var n=t.iterator[e.method];if(n===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=r,_(t,e),"throw"===e.method))return y;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return y}var o=h(n,t.iterator,e.arg);if("throw"===o.type)return e.method="throw",e.arg=o.arg,e.delegate=null,y;var i=o.arg;return i?i.done?(e[t.resultName]=i.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=r),e.delegate=null,y):i:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,y)}function O(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function k(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function S(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(O,this),this.reset(!0)}function N(t){if(t){var e=t[i];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var o=-1,a=function e(){for(;++o<t.length;)if(n.call(t,o))return e.value=t[o],e.done=!1,e;return e.value=r,e.done=!0,e};return a.next=a}}return{next:P}}function P(){return{value:r,done:!0}}return m.prototype=b.constructor=g,g.constructor=m,m.displayName=u(g,c,"GeneratorFunction"),t.isGeneratorFunction=function(t){var r="function"==typeof t&&t.constructor;return!!r&&(r===m||"GeneratorFunction"===(r.displayName||r.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,g):(t.__proto__=g,u(t,c,"GeneratorFunction")),t.prototype=Object.create(b),t},t.awrap=function(t){return{__await:t}},E(j.prototype),j.prototype[a]=function(){return this},t.AsyncIterator=j,t.async=function(r,e,n,o,i){void 0===i&&(i=Promise);var a=new j(s(r,e,n,o),i);return t.isGeneratorFunction(e)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},E(b),u(b,c,"Generator"),b[i]=function(){return this},b.toString=function(){return"[object Generator]"},t.keys=function(t){var r=[];for(var e in t)r.push(e);return r.reverse(),function e(){for(;r.length;){var n=r.pop();if(n in t)return e.value=n,e.done=!1,e}return e.done=!0,e}},t.values=N,S.prototype={constructor:S,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=r,this.done=!1,this.delegate=null,this.method="next",this.arg=r,this.tryEntries.forEach(k),!t)for(var e in this)"t"===e.charAt(0)&&n.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=r)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function o(n,o){return c.type="throw",c.arg=t,e.next=n,o&&(e.method="next",e.arg=r),!!o}for(var i=this.tryEntries.length-1;i>=0;--i){var a=this.tryEntries[i],c=a.completion;if("root"===a.tryLoc)return o("end");if(a.tryLoc<=this.prev){var u=n.call(a,"catchLoc"),s=n.call(a,"finallyLoc");if(u&&s){if(this.prev<a.catchLoc)return o(a.catchLoc,!0);if(this.prev<a.finallyLoc)return o(a.finallyLoc)}else if(u){if(this.prev<a.catchLoc)return o(a.catchLoc,!0)}else{if(!s)throw new Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return o(a.finallyLoc)}}}},abrupt:function(t,r){for(var e=this.tryEntries.length-1;e>=0;--e){var o=this.tryEntries[e];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=r&&r<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=r,i?(this.method="next",this.next=i.finallyLoc,y):this.complete(a)},complete:function(t,r){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&r&&(this.next=r),y},finish:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.finallyLoc===t)return this.complete(e.completion,e.afterLoc),k(e),y}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.tryLoc===t){var n=e.completion;if("throw"===n.type){var o=n.arg;k(e)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:N(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=r),y}},t}(t.exports);try{regeneratorRuntime=r}catch(t){Function("r","regeneratorRuntime = r")(r)}})),u=new Map,s=function(t,e){return void 0===e&&(e={}),function(n){var o=n.lang;process&&process.env&&process.env.TWOSLASH_DISABLE||h(e||{})(n);var i={json5:"json"};i[o]&&(o=i[o]);var a,c,s,l,f,p=n.meta?"string"==typeof n.meta?n.meta:n.meta.join(" "):"";if("twoslash"===o){if(!n.meta)throw new Error("A twoslash code block needs a pragma like 'twoslash include [name]'");c=u,s=n.value,l=p.split(" ")[1],f=[],s.split("\n").forEach((function(t,r){var e=t.trim();if(e.startsWith("// - ")){var n=e.split("// - ")[1].split(" ")[0];c.set(l+"-"+n,f.join("\n"))}else f.push(t)})),c.set(l,f.join("\n")),a=""}else a=t.map((function(t){var e=t.customName.split("/").pop().replace(".json","");return r.renderCodeToHTML(n.value,o,p.split(" "),{themeName:e},t,n.twoslash)})).join("\n");n.type="html",n.value=a,n.children=[]}},h=function(t){return void 0===t&&(t={}),function(e){if(e.meta&&e.meta.includes("twoslash")){var n=function(t,r){for(var e,n=/\/\/ @include: (.*)$/gm,o=[];null!==(e=n.exec(r));){e.index===n.lastIndex&&n.lastIndex++;var i=e[1],a=t.get(i);if(!a){var c="Could not find an includes with the key: '"+i+"'.\nThere is: "+[].concat(t.keys())+".";throw new Error(c)}o.push([e.index,e[0].length,a])}var u=r.toString();return o.reverse().forEach((function(t){u=u.substring(0,t[0])+t[2]+u.substring(t[0]+t[1])})),u}(u,e.value),o=r.runTwoSlash(n,e.lang,t);e.value=o.code,e.lang=o.extension,e.twoslash=o}}};exports.default=function(t){void 0===t&&(t={});var n=t.themes||(t.theme?[t.theme]:["light-plus"]);if(!t.vfsRoot)try{t.vfsRoot=require("path").join(__dirname,"..","..","..")}catch(t){}return function(){var a=o(c.mark((function a(h){var l;return c.wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.next=2,Promise.all(n.map(function(){var e=o(c.mark((function e(n){var o;return c.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,r.createShikiHighlighter(i({},t,{theme:n,themes:void 0}));case 2:return(o=e.sent).customName=n,e.abrupt("return",o);case 5:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}()));case 2:l=a.sent,u.clear(),e(h,"code",s(l,t));case 5:case"end":return a.stop()}}),a)})));return function(t){return a.apply(this,arguments)}}()},exports.runTwoSlashOnNode=h,exports.visitor=s;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,r=require("shiki-twoslash"),e=require("shiki"),n=(t=require("unist-util-visit"))&&"object"==typeof t&&"default"in t?t.default:t;function o(t,r,e,n,o,i,a){try{var c=t[i](a),u=c.value}catch(t){return void e(t)}c.done?r(u):Promise.resolve(u).then(n,o)}function i(t){return function(){var r=this,e=arguments;return new Promise((function(n,i){var a=t.apply(r,e);function c(t){o(a,n,i,c,u,"next",t)}function u(t){o(a,n,i,c,u,"throw",t)}c(void 0)}))}}function a(){return(a=Object.assign||function(t){for(var r=1;r<arguments.length;r++){var e=arguments[r];for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])}return t}).apply(this,arguments)}function c(t,r){return t(r={exports:{}},r.exports),r.exports}var u=c((function(t){var r=function(t){var r,e=Object.prototype,n=e.hasOwnProperty,o="function"==typeof Symbol?Symbol:{},i=o.iterator||"@@iterator",a=o.asyncIterator||"@@asyncIterator",c=o.toStringTag||"@@toStringTag";function u(t,r,e){return Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{u({},"")}catch(t){u=function(t,r,e){return t[r]=e}}function s(t,r,e,n){var o=Object.create((r&&r.prototype instanceof d?r:d).prototype),i=new S(n||[]);return o._invoke=function(t,r,e){var n=l;return function(o,i){if(n===p)throw new Error("Generator is already running");if(n===v){if("throw"===o)throw i;return T()}for(e.method=o,e.arg=i;;){var a=e.delegate;if(a){var c=_(a,e);if(c){if(c===y)continue;return c}}if("next"===e.method)e.sent=e._sent=e.arg;else if("throw"===e.method){if(n===l)throw n=v,e.arg;e.dispatchException(e.arg)}else"return"===e.method&&e.abrupt("return",e.arg);n=p;var u=h(t,r,e);if("normal"===u.type){if(n=e.done?v:f,u.arg===y)continue;return{value:u.arg,done:e.done}}"throw"===u.type&&(n=v,e.method="throw",e.arg=u.arg)}}}(t,e,i),o}function h(t,r,e){try{return{type:"normal",arg:t.call(r,e)}}catch(t){return{type:"throw",arg:t}}}t.wrap=s;var l="suspendedStart",f="suspendedYield",p="executing",v="completed",y={};function d(){}function m(){}function g(){}var w={};w[i]=function(){return this};var x=Object.getPrototypeOf,b=x&&x(x(N([])));b&&b!==e&&n.call(b,i)&&(w=b);var L=g.prototype=d.prototype=Object.create(w);function E(t){["next","throw","return"].forEach((function(r){u(t,r,(function(t){return this._invoke(r,t)}))}))}function j(t,r){var e;this._invoke=function(o,i){function a(){return new r((function(e,a){!function e(o,i,a,c){var u=h(t[o],t,i);if("throw"!==u.type){var s=u.arg,l=s.value;return l&&"object"==typeof l&&n.call(l,"__await")?r.resolve(l.__await).then((function(t){e("next",t,a,c)}),(function(t){e("throw",t,a,c)})):r.resolve(l).then((function(t){s.value=t,a(s)}),(function(t){return e("throw",t,a,c)}))}c(u.arg)}(o,i,e,a)}))}return e=e?e.then(a,a):a()}}function _(t,e){var n=t.iterator[e.method];if(n===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=r,_(t,e),"throw"===e.method))return y;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return y}var o=h(n,t.iterator,e.arg);if("throw"===o.type)return e.method="throw",e.arg=o.arg,e.delegate=null,y;var i=o.arg;return i?i.done?(e[t.resultName]=i.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=r),e.delegate=null,y):i:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,y)}function k(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function O(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function S(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(k,this),this.reset(!0)}function N(t){if(t){var e=t[i];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var o=-1,a=function e(){for(;++o<t.length;)if(n.call(t,o))return e.value=t[o],e.done=!1,e;return e.value=r,e.done=!0,e};return a.next=a}}return{next:T}}function T(){return{value:r,done:!0}}return m.prototype=L.constructor=g,g.constructor=m,m.displayName=u(g,c,"GeneratorFunction"),t.isGeneratorFunction=function(t){var r="function"==typeof t&&t.constructor;return!!r&&(r===m||"GeneratorFunction"===(r.displayName||r.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,g):(t.__proto__=g,u(t,c,"GeneratorFunction")),t.prototype=Object.create(L),t},t.awrap=function(t){return{__await:t}},E(j.prototype),j.prototype[a]=function(){return this},t.AsyncIterator=j,t.async=function(r,e,n,o,i){void 0===i&&(i=Promise);var a=new j(s(r,e,n,o),i);return t.isGeneratorFunction(e)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},E(L),u(L,c,"Generator"),L[i]=function(){return this},L.toString=function(){return"[object Generator]"},t.keys=function(t){var r=[];for(var e in t)r.push(e);return r.reverse(),function e(){for(;r.length;){var n=r.pop();if(n in t)return e.value=n,e.done=!1,e}return e.done=!0,e}},t.values=N,S.prototype={constructor:S,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=r,this.done=!1,this.delegate=null,this.method="next",this.arg=r,this.tryEntries.forEach(O),!t)for(var e in this)"t"===e.charAt(0)&&n.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=r)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function o(n,o){return c.type="throw",c.arg=t,e.next=n,o&&(e.method="next",e.arg=r),!!o}for(var i=this.tryEntries.length-1;i>=0;--i){var a=this.tryEntries[i],c=a.completion;if("root"===a.tryLoc)return o("end");if(a.tryLoc<=this.prev){var u=n.call(a,"catchLoc"),s=n.call(a,"finallyLoc");if(u&&s){if(this.prev<a.catchLoc)return o(a.catchLoc,!0);if(this.prev<a.finallyLoc)return o(a.finallyLoc)}else if(u){if(this.prev<a.catchLoc)return o(a.catchLoc,!0)}else{if(!s)throw new Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return o(a.finallyLoc)}}}},abrupt:function(t,r){for(var e=this.tryEntries.length-1;e>=0;--e){var o=this.tryEntries[e];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=r&&r<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=r,i?(this.method="next",this.next=i.finallyLoc,y):this.complete(a)},complete:function(t,r){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&r&&(this.next=r),y},finish:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.finallyLoc===t)return this.complete(e.completion,e.afterLoc),O(e),y}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.tryLoc===t){var n=e.completion;if("throw"===n.type){var o=n.arg;O(e)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:N(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=r),y}},t}(t.exports);try{regeneratorRuntime=r}catch(t){Function("r","regeneratorRuntime = r")(r)}})),s=new Map,h=function(t,e){return void 0===e&&(e={}),function(n){var o=n.lang;process&&process.env&&process.env.TWOSLASH_DISABLE||l(e||{})(n);var i={json5:"json"};i[o]&&(o=i[o]);var a,c,u,h,f,p=n.meta?"string"==typeof n.meta?n.meta:n.meta.join(" "):"";if("twoslash"===o){if(!n.meta)throw new Error("A twoslash code block needs a pragma like 'twoslash include [name]'");c=s,u=n.value,h=p.split(" ")[1],f=[],u.split("\n").forEach((function(t,r){var e=t.trim();if(e.startsWith("// - ")){var n=e.split("// - ")[1].split(" ")[0];c.set(h+"-"+n,f.join("\n"))}else f.push(t)})),c.set(h,f.join("\n")),a=""}else a=t.map((function(t){var e=t.customName.split("/").pop().replace(".json","");return r.renderCodeToHTML(n.value,o,p.split(" "),{themeName:e},t,n.twoslash)})).join("\n");n.type="html",n.value=a,n.children=[]}},l=function(t){return void 0===t&&(t={}),function(r){if(r.meta&&r.meta.includes("twoslash"))try{var e=function(t,r){for(var e,n=/\/\/ @include: (.*)$/gm,o=[];null!==(e=n.exec(r));){e.index===n.lastIndex&&n.lastIndex++;var i=e[1],a=t.get(i);if(!a){var c="Could not find an includes with the key: '"+i+"'.\nThere is: "+[].concat(t.keys())+".";throw new Error(c)}o.push([e.index,e[0].length,a])}var u=r.toString();return o.reverse().forEach((function(t){u=u.substring(0,t[0])+t[2]+u.substring(t[0]+t[1])})),u}(s,r.value),n=f(e,r.lang,t);r.value=n.code,r.lang=n.extension,r.twoslash=n}catch(t){throw t.message="remark-shiki-twoslash: Error thrown in code sample on line "+(r.position&&r.position.start.line||-1)+"\n\n"+t.message,t}}},f=function(t,e,n){try{require("crypto")}catch(o){return r.runTwoSlash(t,e,n)}var o=require("crypto").createHash,i=require("fs"),a=i.readFileSync,c=i.existsSync,u=i.mkdirSync,s=i.writeFileSync,h=require("path").join,l=o("sha1").update(t).digest("hex"),f=h(__dirname,"..","..",".cache","twoslash"),p=h(f,l+".json");if(c(p))return JSON.parse(a(p,"utf8"));var v=r.runTwoSlash(t,e,n);return c(f)||u(f,{recursive:!0}),s(p,JSON.stringify(v),"utf8"),v},p=new Map;exports.cachedTwoslashCall=f,exports.default=function(t){void 0===t&&(t={});var r=t.themes||(t.theme?[t.theme]:["light-plus"]);if(!t.vfsRoot)try{t.vfsRoot=require("path").join(__dirname,"..","..","..")}catch(t){}return function(){var o=i(u.mark((function o(c){var l;return u.wrap((function(o){for(;;)switch(o.prev=o.next){case 0:return o.next=2,Promise.all(r.map(function(){var r=i(u.mark((function r(n){var o,i,c;return u.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:if(!(i=p.get(o=n.name||n))){r.next=4;break}return r.abrupt("return",i);case 4:return r.next=6,e.getHighlighter(a({},t,{theme:n,themes:void 0}));case 6:return(c=r.sent).customName=o,p.set(o,c),r.abrupt("return",c);case 10:case"end":return r.stop()}}),r)})));return function(t){return r.apply(this,arguments)}}()));case 2:l=o.sent,s.clear(),n(c,"code",h(l,t));case 5:case"end":return o.stop()}}),o)})));return function(t){return o.apply(this,arguments)}}()},exports.runTwoSlashOnNode=l,exports.visitor=h;
//# sourceMappingURL=remark-shiki-twoslash.cjs.production.min.js.map

@@ -1,2 +0,3 @@

import { runTwoSlash, renderCodeToHTML, createShikiHighlighter } from 'shiki-twoslash';
import { renderCodeToHTML, runTwoSlash } from 'shiki-twoslash';
import { getHighlighter } from 'shiki';
import visit from 'unist-util-visit';

@@ -847,5 +848,6 @@

var lang = node.lang;
var settings = twoslashSettings || {};
var shouldDisableTwoslash = process && process.env && !!process.env.TWOSLASH_DISABLE; // Run twoslash
var settings = twoslashSettings || {}; // Offer a way to do high-perf iterations, this is less useful
// given that we cache the results of twoslash in the file-system
var shouldDisableTwoslash = process && process.env && !!process.env.TWOSLASH_DISABLE;
if (!shouldDisableTwoslash) runTwoSlashOnNode(settings)(node); // Shiki doesn't respect json5 as an input, so switch it

@@ -858,5 +860,6 @@ // to json, which can handle comments in the syntax highlight

if (replacer[lang]) lang = replacer[lang];
if (replacer[lang]) lang = replacer[lang]; // The meta is the bit after lang in: ```lang [this bit]
var metaString = !node.meta ? "" : typeof node.meta === "string" ? node.meta : node.meta.join(" ");
var results;
var results; // Support 'twoslash' codesamples

@@ -868,2 +871,3 @@ if (lang === "twoslash") {

} else {
// All good, get each
var output = highlighters.map(function (highlighter) {

@@ -896,11 +900,63 @@ // @ts-ignore

if (node.meta && node.meta.includes("twoslash")) {
var code = replaceIncludesInCode(includes, node.value);
var results = runTwoSlash(code, node.lang, settings);
node.value = results.code;
node.lang = results.extension;
node.twoslash = results;
try {
var code = replaceIncludesInCode(includes, node.value);
var results = cachedTwoslashCall(code, node.lang, settings);
node.value = results.code;
node.lang = results.extension;
node.twoslash = results;
} catch (error) {
var pos = node.position && node.position.start.line || -1;
error.message = "remark-shiki-twoslash: Error thrown in code sample on line " + pos + "\n\n" + error.message;
throw error;
}
}
};
};
/**
* Keeps a cache of the JSON responses to a twoslash call in node_modules/.cache/twoslash
* which should keep CI times down (e.g. the epub vs the handbook etc) - but also during
* dev time, this is useful.
*
*/
var cachedTwoslashCall = function cachedTwoslashCall(code, lang, settings) {
try {
require("crypto");
} catch (err) {
// Not in Node, run un-cached
return runTwoSlash(code, lang, settings);
}
var _require = require("crypto"),
createHash = _require.createHash;
var _require2 = require("fs"),
readFileSync = _require2.readFileSync,
existsSync = _require2.existsSync,
mkdirSync = _require2.mkdirSync,
writeFileSync = _require2.writeFileSync;
var _require3 = require("path"),
join = _require3.join;
var shasum = createHash("sha1");
var codeSha = shasum.update(code).digest("hex");
var cacheRoot = join(__dirname, "..", "..", ".cache", "twoslash");
var cachePath = join(cacheRoot, codeSha + ".json");
if (existsSync(cachePath)) {
return JSON.parse(readFileSync(cachePath, "utf8"));
} else {
var results = runTwoSlash(code, lang, settings);
if (!existsSync(cacheRoot)) mkdirSync(cacheRoot, {
recursive: true
});
writeFileSync(cachePath, JSON.stringify(results), "utf8");
return results;
}
}; // The remark API
// So we only have one highlighter per theme in a process
var highlighterCache = /*#__PURE__*/new Map();
function remarkTwoslash(settings) {

@@ -932,3 +988,3 @@ if (settings === void 0) {

var _ref2 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(theme) {
var highlighter;
var themeName, cached, highlighter;
return runtime_1.wrap(function _callee$(_context) {

@@ -938,4 +994,16 @@ while (1) {

case 0:
_context.next = 2;
return createShikiHighlighter(_extends({}, settings, {
// You can put a string, a path, or the JSON theme obj
themeName = theme.name || theme;
cached = highlighterCache.get(themeName);
if (!cached) {
_context.next = 4;
break;
}
return _context.abrupt("return", cached);
case 4:
_context.next = 6;
return getHighlighter(_extends({}, settings, {
theme: theme,

@@ -945,9 +1013,10 @@ themes: undefined

case 2:
case 6:
highlighter = _context.sent;
// @ts-ignore - https://github.com/shikijs/shiki/pull/162 will fix this
highlighter.customName = theme;
highlighter.customName = themeName;
highlighterCache.set(themeName, highlighter);
return _context.abrupt("return", highlighter);
case 5:
case 10:
case "end":

@@ -987,3 +1056,3 @@ return _context.stop();

export default remarkTwoslash;
export { runTwoSlashOnNode, visitor };
export { cachedTwoslashCall, runTwoSlashOnNode, visitor };
//# sourceMappingURL=remark-shiki-twoslash.esm.js.map
{
"name": "remark-shiki-twoslash",
"version": "1.2.0",
"version": "1.2.1",
"license": "MIT",

@@ -5,0 +5,0 @@ "homepage": "https://github.com/microsoft/TypeScript-Website",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc