remark-shiki-twoslash
Advanced tools
Comparing version 1.0.3 to 1.1.0
@@ -9,3 +9,3 @@ import { UserConfigSettings } from "shiki-twoslash"; | ||
value: string; | ||
meta?: string[]; | ||
meta?: string[] | string; | ||
twoslash?: import("@typescript/twoslash").TwoSlashReturn; | ||
@@ -12,0 +12,0 @@ }; |
@@ -50,5 +50,3 @@ 'use strict'; | ||
var runtime_1 = | ||
/*#__PURE__*/ | ||
createCommonjsModule(function (module) { | ||
var runtime_1 = /*#__PURE__*/createCommonjsModule(function (module) { | ||
/** | ||
@@ -71,2 +69,21 @@ * Copyright (c) 2014-present, Facebook, Inc. | ||
function define(obj, key, value) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
return obj[key]; | ||
} | ||
try { | ||
// IE 8 has a broken Object.defineProperty that only works on DOM objects. | ||
define({}, ""); | ||
} catch (err) { | ||
define = function define(obj, key, value) { | ||
return obj[key] = value; | ||
}; | ||
} | ||
function wrap(innerFn, outerFn, self, tryLocsList) { | ||
@@ -145,3 +162,3 @@ // If outerFn provided and outerFn.prototype is a Generator, then outerFn.prototype instanceof Generator. | ||
GeneratorFunctionPrototype.constructor = GeneratorFunction; | ||
GeneratorFunctionPrototype[toStringTagSymbol] = GeneratorFunction.displayName = "GeneratorFunction"; // Helper for defining the .next, .throw, and .return methods of the | ||
GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"); // Helper for defining the .next, .throw, and .return methods of the | ||
// Iterator interface in terms of a single ._invoke method. | ||
@@ -151,5 +168,5 @@ | ||
["next", "throw", "return"].forEach(function (method) { | ||
prototype[method] = function (arg) { | ||
define(prototype, method, function (arg) { | ||
return this._invoke(method, arg); | ||
}; | ||
}); | ||
}); | ||
@@ -170,6 +187,3 @@ } | ||
genFun.__proto__ = GeneratorFunctionPrototype; | ||
if (!(toStringTagSymbol in genFun)) { | ||
genFun[toStringTagSymbol] = "GeneratorFunction"; | ||
} | ||
define(genFun, toStringTagSymbol, "GeneratorFunction"); | ||
} | ||
@@ -191,3 +205,3 @@ | ||
function AsyncIterator(generator) { | ||
function AsyncIterator(generator, PromiseImpl) { | ||
function invoke(method, arg, resolve, reject) { | ||
@@ -203,3 +217,3 @@ var record = tryCatch(generator[method], generator, arg); | ||
if (value && typeof value === "object" && hasOwn.call(value, "__await")) { | ||
return Promise.resolve(value.__await).then(function (value) { | ||
return PromiseImpl.resolve(value.__await).then(function (value) { | ||
invoke("next", value, resolve, reject); | ||
@@ -211,3 +225,3 @@ }, function (err) { | ||
return Promise.resolve(value).then(function (unwrapped) { | ||
return PromiseImpl.resolve(value).then(function (unwrapped) { | ||
// When a yielded Promise is resolved, its final value becomes | ||
@@ -230,3 +244,3 @@ // the .value of the Promise<{value,done}> result for the | ||
function callInvokeWithMethodAndArg() { | ||
return new Promise(function (resolve, reject) { | ||
return new PromiseImpl(function (resolve, reject) { | ||
invoke(method, arg, resolve, reject); | ||
@@ -268,4 +282,5 @@ }); | ||
exports.async = function (innerFn, outerFn, self, tryLocsList) { | ||
var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList)); | ||
exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) { | ||
if (PromiseImpl === void 0) PromiseImpl = Promise; | ||
var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl); | ||
return exports.isGeneratorFunction(outerFn) ? iter // If outerFn is a generator, return the full iterator. | ||
@@ -434,3 +449,3 @@ : iter.next().then(function (result) { | ||
defineIteratorMethods(Gp); | ||
Gp[toStringTagSymbol] = "Generator"; // A Generator should always return itself as the iterator object when the | ||
define(Gp, toStringTagSymbol, "Generator"); // A Generator should always return itself as the iterator object when the | ||
// @@iterator function is called on it. Some browsers' implementations of the | ||
@@ -770,2 +785,51 @@ // iterator prototype chain incorrectly implement this, causing the Generator | ||
var addIncludes = function addIncludes(map, code, metaInfo) { | ||
var name = metaInfo.split(" ")[1]; | ||
var lines = []; | ||
code.split("\n").forEach(function (l, _i) { | ||
var trimmed = l.trim(); | ||
if (trimmed.startsWith("// - ")) { | ||
var key = trimmed.split("// - ")[1].split(" ")[0]; | ||
map.set(name + "-" + key, lines.join("\n")); | ||
} else { | ||
lines.push(l); | ||
} | ||
}); | ||
map.set(name, lines.join("\n")); | ||
}; | ||
var replaceIncludesInCode = function replaceIncludesInCode(_map, code) { | ||
var includes = /\/\/ @include: (.*)$/gm; // Basically run a regex over the code replacing any // @include: thing with | ||
// 'thing' from the map | ||
var toReplace = []; | ||
var match; | ||
while ((match = includes.exec(code)) !== null) { | ||
// This is necessary to avoid infinite loops with zero-width matches | ||
if (match.index === includes.lastIndex) { | ||
includes.lastIndex++; | ||
} | ||
var key = match[1]; | ||
var replaceWith = _map.get(key); | ||
if (!replaceWith) { | ||
var msg = "Could not find an includes with the key: '" + key + "'.\nThere is: " + [].concat(_map.keys()) + "."; | ||
throw new Error(msg); | ||
} | ||
toReplace.push([match.index, match[0].length, replaceWith]); | ||
} | ||
var newCode = code.toString(); // Go backwards through the found changes so that we can retain index position | ||
toReplace.reverse().forEach(function (r) { | ||
newCode = newCode.substring(0, r[0]) + r[2] + newCode.substring(r[0] + r[1]); | ||
}); | ||
return newCode; | ||
}; | ||
var includes = /*#__PURE__*/new Map(); | ||
/** | ||
@@ -794,3 +858,13 @@ * The function doing the work of transforming any codeblock samples | ||
if (replacer[lang]) lang = replacer[lang]; | ||
var results = shikiTwoslash.renderCodeToHTML(node.value, lang, node.meta || [], {}, highlighter, node.twoslash); | ||
var results; | ||
if (lang === "twoslash") { | ||
if (!node.meta) throw new Error("A twoslash code block needs a pragma like 'twoslash include [name]'"); | ||
var metaInfo = typeof node.meta === "string" ? node.meta : node.meta.join(" "); | ||
addIncludes(includes, node.value, metaInfo || ""); | ||
results = ""; | ||
} else { | ||
results = shikiTwoslash.renderCodeToHTML(node.value, lang, node.meta, {}, highlighter, node.twoslash); | ||
} | ||
node.type = "html"; | ||
@@ -813,3 +887,4 @@ node.value = results; | ||
if (node.meta && node.meta.includes("twoslash")) { | ||
var results = shikiTwoslash.runTwoSlash(node.value, node.lang, settings); | ||
var code = replaceIncludesInCode(includes, node.value); | ||
var results = shikiTwoslash.runTwoSlash(code, node.lang, settings); | ||
node.value = results.code; | ||
@@ -830,10 +905,14 @@ node.lang = results.extension; | ||
theme: "light-plus" | ||
}; | ||
}; // Default to assuming you want vfs node_modules set up | ||
// but don't assume you're on node though | ||
var transform = | ||
/*#__PURE__*/ | ||
function () { | ||
var _ref = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
runtime_1.mark(function _callee(markdownAST) { | ||
if (!settings["vfsRoot"]) { | ||
try { | ||
// dist > remark-shiki-twoslash > node_modules | ||
settings.vfsRoot = require("path").join(__dirname, "..", "..", ".."); | ||
} catch (error) {} | ||
} | ||
var transform = /*#__PURE__*/function () { | ||
var _ref = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(markdownAST) { | ||
var highlighter; | ||
@@ -849,5 +928,6 @@ return runtime_1.wrap(function _callee$(_context) { | ||
highlighter = _context.sent; | ||
includes.clear(); | ||
visit(markdownAST, "code", visitor(highlighter, settings)); | ||
case 4: | ||
case 5: | ||
case "end": | ||
@@ -854,0 +934,0 @@ return _context.stop(); |
@@ -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,r){return t(r={exports:{}},r.exports),r.exports}var i=o((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,n){var o=Object.create((r&&r.prototype instanceof y?r:y).prototype),i=new S(n||[]);return o._invoke=function(t,r,e){var n=h;return function(o,i){if(n===l)throw new Error("Generator is already running");if(n===p){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===v)continue;return c}}if("next"===e.method)e.sent=e._sent=e.arg;else if("throw"===e.method){if(n===h)throw n=p,e.arg;e.dispatchException(e.arg)}else"return"===e.method&&e.abrupt("return",e.arg);n=l;var u=s(t,r,e);if("normal"===u.type){if(n=e.done?p:f,u.arg===v)continue;return{value:u.arg,done:e.done}}"throw"===u.type&&(n=p,e.method="throw",e.arg=u.arg)}}}(t,e,i),o}function s(t,r,e){try{return{type:"normal",arg:t.call(r,e)}}catch(t){return{type:"throw",arg:t}}}t.wrap=u;var h="suspendedStart",f="suspendedYield",l="executing",p="completed",v={};function y(){}function d(){}function g(){}var m={};m[i]=function(){return this};var w=Object.getPrototypeOf,x=w&&w(w(k([])));x&&x!==e&&n.call(x,i)&&(m=x);var L=g.prototype=y.prototype=Object.create(m);function E(t){["next","throw","return"].forEach((function(r){t[r]=function(t){return this._invoke(r,t)}}))}function b(t){var r;this._invoke=function(e,o){function i(){return new Promise((function(r,i){!function r(e,o,i,a){var c=s(t[e],t,o);if("throw"!==c.type){var u=c.arg,h=u.value;return h&&"object"==typeof h&&n.call(h,"__await")?Promise.resolve(h.__await).then((function(t){r("next",t,i,a)}),(function(t){r("throw",t,i,a)})):Promise.resolve(h).then((function(t){u.value=t,i(u)}),(function(t){return r("throw",t,i,a)}))}a(c.arg)}(e,o,r,i)}))}return r=r?r.then(i,i):i()}}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 v;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return v}var o=s(n,t.iterator,e.arg);if("throw"===o.type)return e.method="throw",e.arg=o.arg,e.delegate=null,v;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,v):i:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,v)}function j(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(j,this),this.reset(!0)}function k(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 d.prototype=L.constructor=g,g.constructor=d,g[c]=d.displayName="GeneratorFunction",t.isGeneratorFunction=function(t){var r="function"==typeof t&&t.constructor;return!!r&&(r===d||"GeneratorFunction"===(r.displayName||r.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,g):(t.__proto__=g,c in t||(t[c]="GeneratorFunction")),t.prototype=Object.create(L),t},t.awrap=function(t){return{__await:t}},E(b.prototype),b.prototype[a]=function(){return this},t.AsyncIterator=b,t.async=function(r,e,n,o){var i=new b(u(r,e,n,o));return t.isGeneratorFunction(e)?i:i.next().then((function(t){return t.done?t.value:i.next()}))},E(L),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=k,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,v):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),v},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),v}},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:k(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=r),v}},t}(t.exports);try{regeneratorRuntime=r}catch(t){Function("r","regeneratorRuntime = r")(r)}})),a=function(t,e){return void 0===e&&(e={}),function(n){var o=n.lang;process&&process.env&&process.env.TWOSLASH_DISABLE||c(e||{})(n);var i={json5:"json"};i[o]&&(o=i[o]);var a=r.renderCodeToHTML(n.value,o,n.meta||[],{},t,n.twoslash);n.type="html",n.value=a,n.children=[]}},c=function(t){return void 0===t&&(t={}),function(e){if(e.meta&&e.meta.includes("twoslash")){var n=r.runTwoSlash(e.value,e.lang,t);e.value=n.code,e.lang=n.extension,e.twoslash=n}}};exports.default=function(t){void 0===t&&(t={});var o=t||{theme:"light-plus"};return function(){var t,c=(t=i.mark((function t(n){return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,r.createShikiHighlighter(o);case 2:e(n,"code",a(t.sent,o));case 4:case"end":return t.stop()}}),t)})),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)}))});return function(t){return c.apply(this,arguments)}}()},exports.runTwoSlashOnNode=c,exports.visitor=a; | ||
"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,r){return t(r={exports:{}},r.exports),r.exports}var i=o((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 N()}for(e.method=o,e.arg=i;;){var a=e.delegate;if(a){var c=j(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 g(){}function m(){}var w={};w[i]=function(){return this};var x=Object.getPrototypeOf,L=x&&x(x(T([])));L&&L!==e&&n.call(L,i)&&(w=L);var E=m.prototype=d.prototype=Object.create(w);function b(t){["next","throw","return"].forEach((function(r){u(t,r,(function(t){return this._invoke(r,t)}))}))}function _(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 j(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,j(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 T(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:N}}function N(){return{value:r,done:!0}}return g.prototype=E.constructor=m,m.constructor=g,g.displayName=u(m,c,"GeneratorFunction"),t.isGeneratorFunction=function(t){var r="function"==typeof t&&t.constructor;return!!r&&(r===g||"GeneratorFunction"===(r.displayName||r.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,m):(t.__proto__=m,u(t,c,"GeneratorFunction")),t.prototype=Object.create(E),t},t.awrap=function(t){return{__await:t}},b(_.prototype),_.prototype[a]=function(){return this},t.AsyncIterator=_,t.async=function(r,e,n,o,i){void 0===i&&(i=Promise);var a=new _(s(r,e,n,o),i);return t.isGeneratorFunction(e)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},b(E),u(E,c,"Generator"),E[i]=function(){return this},E.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=T,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:T(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)}})),a=new Map,c=function(t,e){return void 0===e&&(e={}),function(n){var o=n.lang;process&&process.env&&process.env.TWOSLASH_DISABLE||u(e||{})(n);var i,c={json5:"json"};if(c[o]&&(o=c[o]),"twoslash"===o){if(!n.meta)throw new Error("A twoslash code block needs a pragma like 'twoslash include [name]'");var s="string"==typeof n.meta?n.meta:n.meta.join(" ");!function(t,r,e){var n=e.split(" ")[1],o=[];r.split("\n").forEach((function(r,e){var i=r.trim();if(i.startsWith("// - ")){var a=i.split("// - ")[1].split(" ")[0];t.set(n+"-"+a,o.join("\n"))}else o.push(r)})),t.set(n,o.join("\n"))}(a,n.value,s||""),i=""}else i=r.renderCodeToHTML(n.value,o,n.meta,{},t,n.twoslash);n.type="html",n.value=i,n.children=[]}},u=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}(a,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 o=t||{theme:"light-plus"};if(!o.vfsRoot)try{o.vfsRoot=require("path").join(__dirname,"..","..","..")}catch(t){}return function(){var t,u=(t=i.mark((function t(n){var u;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,r.createShikiHighlighter(o);case 2:u=t.sent,a.clear(),e(n,"code",c(u,o));case 5:case"end":return t.stop()}}),t)})),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)}))});return function(t){return u.apply(this,arguments)}}()},exports.runTwoSlashOnNode=u,exports.visitor=c; | ||
//# sourceMappingURL=remark-shiki-twoslash.cjs.production.min.js.map |
@@ -44,5 +44,3 @@ import { runTwoSlash, renderCodeToHTML, createShikiHighlighter } from 'shiki-twoslash'; | ||
var runtime_1 = | ||
/*#__PURE__*/ | ||
createCommonjsModule(function (module) { | ||
var runtime_1 = /*#__PURE__*/createCommonjsModule(function (module) { | ||
/** | ||
@@ -65,2 +63,21 @@ * Copyright (c) 2014-present, Facebook, Inc. | ||
function define(obj, key, value) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
return obj[key]; | ||
} | ||
try { | ||
// IE 8 has a broken Object.defineProperty that only works on DOM objects. | ||
define({}, ""); | ||
} catch (err) { | ||
define = function define(obj, key, value) { | ||
return obj[key] = value; | ||
}; | ||
} | ||
function wrap(innerFn, outerFn, self, tryLocsList) { | ||
@@ -139,3 +156,3 @@ // If outerFn provided and outerFn.prototype is a Generator, then outerFn.prototype instanceof Generator. | ||
GeneratorFunctionPrototype.constructor = GeneratorFunction; | ||
GeneratorFunctionPrototype[toStringTagSymbol] = GeneratorFunction.displayName = "GeneratorFunction"; // Helper for defining the .next, .throw, and .return methods of the | ||
GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"); // Helper for defining the .next, .throw, and .return methods of the | ||
// Iterator interface in terms of a single ._invoke method. | ||
@@ -145,5 +162,5 @@ | ||
["next", "throw", "return"].forEach(function (method) { | ||
prototype[method] = function (arg) { | ||
define(prototype, method, function (arg) { | ||
return this._invoke(method, arg); | ||
}; | ||
}); | ||
}); | ||
@@ -164,6 +181,3 @@ } | ||
genFun.__proto__ = GeneratorFunctionPrototype; | ||
if (!(toStringTagSymbol in genFun)) { | ||
genFun[toStringTagSymbol] = "GeneratorFunction"; | ||
} | ||
define(genFun, toStringTagSymbol, "GeneratorFunction"); | ||
} | ||
@@ -185,3 +199,3 @@ | ||
function AsyncIterator(generator) { | ||
function AsyncIterator(generator, PromiseImpl) { | ||
function invoke(method, arg, resolve, reject) { | ||
@@ -197,3 +211,3 @@ var record = tryCatch(generator[method], generator, arg); | ||
if (value && typeof value === "object" && hasOwn.call(value, "__await")) { | ||
return Promise.resolve(value.__await).then(function (value) { | ||
return PromiseImpl.resolve(value.__await).then(function (value) { | ||
invoke("next", value, resolve, reject); | ||
@@ -205,3 +219,3 @@ }, function (err) { | ||
return Promise.resolve(value).then(function (unwrapped) { | ||
return PromiseImpl.resolve(value).then(function (unwrapped) { | ||
// When a yielded Promise is resolved, its final value becomes | ||
@@ -224,3 +238,3 @@ // the .value of the Promise<{value,done}> result for the | ||
function callInvokeWithMethodAndArg() { | ||
return new Promise(function (resolve, reject) { | ||
return new PromiseImpl(function (resolve, reject) { | ||
invoke(method, arg, resolve, reject); | ||
@@ -262,4 +276,5 @@ }); | ||
exports.async = function (innerFn, outerFn, self, tryLocsList) { | ||
var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList)); | ||
exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) { | ||
if (PromiseImpl === void 0) PromiseImpl = Promise; | ||
var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl); | ||
return exports.isGeneratorFunction(outerFn) ? iter // If outerFn is a generator, return the full iterator. | ||
@@ -428,3 +443,3 @@ : iter.next().then(function (result) { | ||
defineIteratorMethods(Gp); | ||
Gp[toStringTagSymbol] = "Generator"; // A Generator should always return itself as the iterator object when the | ||
define(Gp, toStringTagSymbol, "Generator"); // A Generator should always return itself as the iterator object when the | ||
// @@iterator function is called on it. Some browsers' implementations of the | ||
@@ -764,2 +779,51 @@ // iterator prototype chain incorrectly implement this, causing the Generator | ||
var addIncludes = function addIncludes(map, code, metaInfo) { | ||
var name = metaInfo.split(" ")[1]; | ||
var lines = []; | ||
code.split("\n").forEach(function (l, _i) { | ||
var trimmed = l.trim(); | ||
if (trimmed.startsWith("// - ")) { | ||
var key = trimmed.split("// - ")[1].split(" ")[0]; | ||
map.set(name + "-" + key, lines.join("\n")); | ||
} else { | ||
lines.push(l); | ||
} | ||
}); | ||
map.set(name, lines.join("\n")); | ||
}; | ||
var replaceIncludesInCode = function replaceIncludesInCode(_map, code) { | ||
var includes = /\/\/ @include: (.*)$/gm; // Basically run a regex over the code replacing any // @include: thing with | ||
// 'thing' from the map | ||
var toReplace = []; | ||
var match; | ||
while ((match = includes.exec(code)) !== null) { | ||
// This is necessary to avoid infinite loops with zero-width matches | ||
if (match.index === includes.lastIndex) { | ||
includes.lastIndex++; | ||
} | ||
var key = match[1]; | ||
var replaceWith = _map.get(key); | ||
if (!replaceWith) { | ||
var msg = "Could not find an includes with the key: '" + key + "'.\nThere is: " + [].concat(_map.keys()) + "."; | ||
throw new Error(msg); | ||
} | ||
toReplace.push([match.index, match[0].length, replaceWith]); | ||
} | ||
var newCode = code.toString(); // Go backwards through the found changes so that we can retain index position | ||
toReplace.reverse().forEach(function (r) { | ||
newCode = newCode.substring(0, r[0]) + r[2] + newCode.substring(r[0] + r[1]); | ||
}); | ||
return newCode; | ||
}; | ||
var includes = /*#__PURE__*/new Map(); | ||
/** | ||
@@ -788,3 +852,13 @@ * The function doing the work of transforming any codeblock samples | ||
if (replacer[lang]) lang = replacer[lang]; | ||
var results = renderCodeToHTML(node.value, lang, node.meta || [], {}, highlighter, node.twoslash); | ||
var results; | ||
if (lang === "twoslash") { | ||
if (!node.meta) throw new Error("A twoslash code block needs a pragma like 'twoslash include [name]'"); | ||
var metaInfo = typeof node.meta === "string" ? node.meta : node.meta.join(" "); | ||
addIncludes(includes, node.value, metaInfo || ""); | ||
results = ""; | ||
} else { | ||
results = renderCodeToHTML(node.value, lang, node.meta, {}, highlighter, node.twoslash); | ||
} | ||
node.type = "html"; | ||
@@ -807,3 +881,4 @@ node.value = results; | ||
if (node.meta && node.meta.includes("twoslash")) { | ||
var results = runTwoSlash(node.value, node.lang, settings); | ||
var code = replaceIncludesInCode(includes, node.value); | ||
var results = runTwoSlash(code, node.lang, settings); | ||
node.value = results.code; | ||
@@ -824,10 +899,14 @@ node.lang = results.extension; | ||
theme: "light-plus" | ||
}; | ||
}; // Default to assuming you want vfs node_modules set up | ||
// but don't assume you're on node though | ||
var transform = | ||
/*#__PURE__*/ | ||
function () { | ||
var _ref = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
runtime_1.mark(function _callee(markdownAST) { | ||
if (!settings["vfsRoot"]) { | ||
try { | ||
// dist > remark-shiki-twoslash > node_modules | ||
settings.vfsRoot = require("path").join(__dirname, "..", "..", ".."); | ||
} catch (error) {} | ||
} | ||
var transform = /*#__PURE__*/function () { | ||
var _ref = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(markdownAST) { | ||
var highlighter; | ||
@@ -843,5 +922,6 @@ return runtime_1.wrap(function _callee$(_context) { | ||
highlighter = _context.sent; | ||
includes.clear(); | ||
visit(markdownAST, "code", visitor(highlighter, settings)); | ||
case 4: | ||
case 5: | ||
case "end": | ||
@@ -848,0 +928,0 @@ return _context.stop(); |
{ | ||
"name": "remark-shiki-twoslash", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/microsoft/TypeScript-Website", |
@@ -1,5 +0,5 @@ | ||
### gatsby-shiki-twoslash | ||
### remark-shiki-twoslash | ||
Sets up markdown code blocks to run through [shiki](https://shiki.matsu.io) which means it gets the VS Code quality | ||
syntax highlighting. This code is basically the same as [gatsby-remark-shiki-twoslash](https://www.gatsbyjs.org/packages/gatsby-remark-shiki-thoslash/). | ||
syntax highlighting, with optional inline TypeScript compiler-backed tooling. | ||
@@ -14,3 +14,3 @@ Why Shiki? Shiki uses the same syntax highlighter engine as VS Code, which means no matter how complex your code is - it will syntax highlight correctly. | ||
With a bit of work, you can explain complicated code in a way that lets people introspect at their own pace. | ||
With Shiki Twoslash, you can explain complicated code in a way that lets people introspect at their own pace. | ||
@@ -22,3 +22,3 @@ ## Plugin Setup | ||
1. **Install the dependency**: `yarn add remark-shiki-twoslash` | ||
1. **Include `"gatsby-remark-shiki-twoslash"` in the plugins section** of whatever you're using: | ||
1. **Include `"remark-shiki-twoslash"` in the plugins section** of whatever you're using: | ||
@@ -154,3 +154,3 @@ ```diff | ||
import React, { useEffect } from "react" | ||
import { setupTwoslashHovers } from "shiki-twoslash/dom"; | ||
import { setupTwoslashHovers } from "shiki-twoslash/dist/dom"; | ||
@@ -213,1 +213,50 @@ export default () => { | ||
[set `theme`](https://github.com/octref/shiki/blob/master/packages/themes/README.md#shiki-themes), then also the [TwoslashOptions here](https://www.npmjs.com/package/@typescript/twoslash#api-1). | ||
### Power User Features | ||
Once you start writing long articles, you'll start to feel the desire to remove repetition in your code samples. This plugin adds the ability to import code into code samples. This is a string replacement before code is passed to twoslash. This is done by making a `twoslash include` code sample which is given a unique identifier. | ||
Inside that code-block, you can use `// - [id]` to make sub-queries to the import, these will be stripped out in the code show. Here's an example markdown file using `includes`: | ||
````markdown | ||
# Hello, world! | ||
```twoslash include main | ||
const a = 1 | ||
// - 1 | ||
const b = 2 | ||
// - 2 | ||
const c= 3 | ||
``` | ||
Let's talk a bit about `a`: | ||
```ts twoslash | ||
// @include: main-1 | ||
``` | ||
`a` can be added to another number | ||
```ts twoslash | ||
// @include: main-1 | ||
// ---cut--- | ||
const nextA = a + 13 | ||
``` | ||
You can see what happens when you add `a + b` | ||
```ts twoslash | ||
// @include: main-2 | ||
// ---cut--- | ||
const result = a + b | ||
// ^? | ||
``` | ||
Finally here is `c`: | ||
```ts twoslash | ||
// @include: main | ||
// ---cut--- | ||
c.toString() | ||
``` | ||
```` |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
234600
12
1600
259