版博士V2.0程序
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

354 linhas
9.6 KiB

  1. // src/index.ts
  2. import * as fs from "fs";
  3. // src/engines.ts
  4. import yaml from "js-yaml";
  5. // src/engine.ts
  6. function aliase(name) {
  7. switch (name.toLowerCase()) {
  8. case "js":
  9. case "javascript": {
  10. return "javascript";
  11. }
  12. case "coffee":
  13. case "coffeescript":
  14. case "cson": {
  15. return "coffee";
  16. }
  17. case "yaml":
  18. case "yml": {
  19. return "yaml";
  20. }
  21. default: {
  22. return name;
  23. }
  24. }
  25. }
  26. var engine = (name, options3) => {
  27. let engine2 = options3.engines[name] || options3.engines[aliase(name)];
  28. if (engine2 === void 0) {
  29. throw new TypeError('gray-matter engine "' + name + '" is not registered');
  30. }
  31. if (typeof engine2 === "function") {
  32. engine2 = { parse: engine2 };
  33. }
  34. return engine2;
  35. };
  36. var engine_default = engine;
  37. // src/parse.ts
  38. var parse = (language, str2, options3) => {
  39. const opts = defaults_default(options3);
  40. const engine2 = engine_default(language, opts);
  41. if (typeof engine2.parse !== "function") {
  42. throw new TypeError('expected "' + language + '.parse" to be a function');
  43. }
  44. return engine2.parse(str2, opts);
  45. };
  46. var parse_default = parse;
  47. // src/engines.ts
  48. var engines = {
  49. yaml: {
  50. parse: yaml.load.bind(yaml),
  51. stringify: yaml.dump.bind(yaml)
  52. },
  53. json: {
  54. parse: JSON.parse.bind(JSON),
  55. stringify: function(obj, options3) {
  56. const opts = Object.assign({ replacer: null, space: 2 }, options3);
  57. return JSON.stringify(obj, opts.replacer, opts.space);
  58. }
  59. },
  60. javascript(str, options, wrap) {
  61. try {
  62. if (wrap !== false) {
  63. str = "(function() {\nreturn " + str.trim() + ";\n}());";
  64. }
  65. return eval(str) || {};
  66. } catch (error) {
  67. if (wrap !== false && /(unexpected|identifier)/i.test(error.message)) {
  68. return parse_default(str, options, false);
  69. }
  70. throw new SyntaxError(error.message);
  71. }
  72. },
  73. stringify() {
  74. throw new Error("stringifying JavaScript is not supported");
  75. }
  76. };
  77. var engines_default = engines;
  78. // src/utils.ts
  79. import stripBom from "strip-bom-string";
  80. import typeOf from "kind-of";
  81. var utils = {
  82. define: (obj, key, val) => {
  83. Reflect.defineProperty(obj, key, {
  84. enumerable: false,
  85. configurable: true,
  86. writable: true,
  87. value: val
  88. });
  89. },
  90. isBuffer: (val) => {
  91. return typeOf(val) === "buffer";
  92. },
  93. isObject: (val) => {
  94. return typeOf(val) === "object";
  95. },
  96. toBuffer(input) {
  97. return typeof input === "string" ? Buffer.from(input) : input;
  98. },
  99. toString(input) {
  100. if (utils.isBuffer(input)) {
  101. return stripBom(String(input));
  102. }
  103. if (typeof input !== "string") {
  104. throw new TypeError("expected input to be a string or buffer");
  105. }
  106. return stripBom(input);
  107. },
  108. arrayify(val) {
  109. return val ? Array.isArray(val) ? val : [val] : [];
  110. },
  111. startsWith(str2, substr, len) {
  112. if (typeof len !== "number") {
  113. len = substr.length;
  114. }
  115. return str2.slice(0, len) === substr;
  116. }
  117. };
  118. // src/defaults.ts
  119. var options2 = (options3) => {
  120. const opts = Object.assign({}, options3);
  121. opts.delimiters = utils.arrayify(opts.delims || opts.delimiters || "---");
  122. if (opts.delimiters.length === 1) {
  123. opts.delimiters.push(opts.delimiters[0]);
  124. }
  125. opts.language = (opts.language || opts.lang || "yaml").toLowerCase();
  126. opts.engines = Object.assign({}, engines_default, opts.parser, opts.engines);
  127. return opts;
  128. };
  129. var defaults_default = options2;
  130. // src/stringify.ts
  131. import typeOf2 from "kind-of";
  132. function newline(str2) {
  133. return str2.slice(-1) === "\n" ? str2 : str2 + "\n";
  134. }
  135. var stringify = (file, data = {}, options3 = {}) => {
  136. if (data == null && options3 == null) {
  137. switch (typeOf2(file)) {
  138. case "object": {
  139. data = file.data;
  140. options3 = {};
  141. break;
  142. }
  143. case "string": {
  144. return file;
  145. }
  146. default: {
  147. throw new TypeError("expected file to be a string or object");
  148. }
  149. }
  150. }
  151. const str2 = file.content;
  152. const opts = defaults_default(options3);
  153. if (data == null) {
  154. if (!opts.data) {
  155. return file;
  156. }
  157. data = opts?.data;
  158. }
  159. const language = file.language || opts.language;
  160. const engine2 = engine_default(language, opts);
  161. if (typeof engine2.stringify !== "function") {
  162. throw new TypeError('expected "' + language + '.stringify" to be a function');
  163. }
  164. data = Object.assign({}, file.data, data);
  165. const open = opts?.delimiters ? opts.delimiters[0] : opts?.delims ? opts?.delims[0] : void 0;
  166. const close = opts?.delimiters ? opts.delimiters[1] ? opts.delimiters[1] : opts?.delims ? opts.delims[1] : void 0 : void 0;
  167. const matter2 = engine2.stringify(data, options3).trim();
  168. let buf = "";
  169. if (matter2 !== "{}") {
  170. buf = newline(open) + newline(matter2) + newline(close);
  171. }
  172. if (typeof file.excerpt === "string" && file.excerpt !== "" && !str2.includes(file.excerpt.trim())) {
  173. buf += newline(file.excerpt) + newline(close);
  174. }
  175. return buf + newline(str2);
  176. };
  177. var stringify_default = stringify;
  178. // src/excerpt.ts
  179. var excerpt = (file, options3) => {
  180. const opts = defaults_default(options3);
  181. if (file.data == null) {
  182. file.data = {};
  183. }
  184. if (typeof opts.excerpt === "function") {
  185. return opts.excerpt(file, opts);
  186. }
  187. const sep = file.data.excerpt_separator || opts.excerpt_separator;
  188. if (sep == null && (opts.excerpt === false || opts.excerpt == null)) {
  189. return file;
  190. }
  191. const delimiter = typeof opts.excerpt === "string" ? opts.excerpt : sep || opts.delimiters[0];
  192. const idx = file.content.indexOf(delimiter);
  193. if (idx !== -1) {
  194. file.excerpt = file.content.slice(0, idx);
  195. }
  196. return file;
  197. };
  198. var excerpt_default = excerpt;
  199. // src/to-file.ts
  200. import typeOf3 from "kind-of";
  201. var toFile = (file) => {
  202. if (typeOf3(file) !== "object") {
  203. file = { content: file };
  204. }
  205. if (typeOf3(file.data) !== "object") {
  206. file.data = {};
  207. }
  208. if (file.contents && file.content == null) {
  209. file.content = file.contents;
  210. }
  211. utils.define(file, "orig", utils.toBuffer(file.content));
  212. utils.define(file, "language", file.language || "");
  213. utils.define(file, "matter", file.matter || "");
  214. utils.define(file, "stringify", (data, options3) => {
  215. if (options3 && options3.language) {
  216. file.language = options3.language;
  217. }
  218. return stringify_default(file, data, options3);
  219. });
  220. file.content = utils.toString(file.content);
  221. file.isEmpty = false;
  222. file.excerpt = "";
  223. return file;
  224. };
  225. var to_file_default = toFile;
  226. // src/index.ts
  227. import { createFnWithProps } from "inferred-types";
  228. import sections from "section-matter";
  229. var matterDict = {
  230. cache: {},
  231. read(filepath, options3) {
  232. const str2 = fs.readFileSync(filepath, "utf8");
  233. const file = matterFn(str2, options3);
  234. file.path = filepath;
  235. return file;
  236. },
  237. stringify(file, data, options3) {
  238. return typeof file === "string" ? stringify_default(matterFn(file, options3), data, options3) : stringify_default(file, data, options3);
  239. },
  240. language(str2, options3) {
  241. const opts = defaults_default(options3);
  242. const open = opts?.delimiters ? opts.delimiters[0] : "---";
  243. if (matterDict.test(str2)) {
  244. str2 = str2.slice(open.length);
  245. }
  246. const language = str2.slice(0, str2.search(/\r?\n/));
  247. return {
  248. raw: language,
  249. name: language ? language.trim() : ""
  250. };
  251. },
  252. parseMatter(file, options3) {
  253. const opts = defaults_default(options3);
  254. const open = opts.delimiters ? opts.delimiters[0] : "---";
  255. const close = "\n" + opts.delimiters[1];
  256. const f = typeof file === "string" ? {
  257. content: file,
  258. path: "",
  259. language: "",
  260. data: {},
  261. contents: void 0,
  262. matter: "",
  263. orig: ""
  264. } : file;
  265. if (opts.language) {
  266. f.language = opts.language;
  267. }
  268. const openLen = open.length;
  269. if (!utils.startsWith(f.content, open, openLen)) {
  270. excerpt_default(file, opts);
  271. return file;
  272. }
  273. if (f.content.charAt(openLen) === open.slice(-1)) {
  274. return file;
  275. }
  276. f.content = f.content.slice(openLen);
  277. const len = f.content.length;
  278. const language = matter.language(f.content, opts) || opts.language;
  279. if (language.name) {
  280. f.language = language.name;
  281. f.content = f.content.slice(language.raw.length);
  282. }
  283. let closeIndex = f.content.indexOf(close);
  284. if (closeIndex === -1) {
  285. closeIndex = len;
  286. }
  287. f.matter = f.content.slice(0, closeIndex);
  288. const block = f.matter.replace(/^\s*#[^\n]+/gm, "").trim();
  289. if (block === "") {
  290. f.isEmpty = true;
  291. f.empty = f.content;
  292. f.data = {};
  293. } else {
  294. f.data = parse_default(f.language, f.matter, opts);
  295. }
  296. if (closeIndex === len) {
  297. f.content = "";
  298. } else {
  299. f.content = f.content.slice(closeIndex + close.length);
  300. if (f.content[0] === "\r") {
  301. f.content = f.content.slice(1);
  302. }
  303. if (f.content[0] === "\n") {
  304. f.content = f.content.slice(1);
  305. }
  306. }
  307. excerpt_default(f, opts);
  308. if (opts?.sections === true || typeof opts.section === "function") {
  309. sections(f, opts.section);
  310. }
  311. return f;
  312. },
  313. test(str2, options3) {
  314. return utils.startsWith(str2, defaults_default(options3).delimiters[0]);
  315. },
  316. clearCache() {
  317. matterDict.cache = {};
  318. }
  319. };
  320. var matterFn = (input, options3) => {
  321. if (input === "") {
  322. return { data: {}, content: input, excerpt: "", orig: input };
  323. }
  324. let file = to_file_default(input);
  325. const cached = matterDict.cache[file.content];
  326. if (!options3) {
  327. if (cached) {
  328. file = Object.assign({}, cached);
  329. file.orig = cached.orig;
  330. return file;
  331. }
  332. matterDict.cache[file.content] = file;
  333. }
  334. return matterDict.parseMatter(file, options3);
  335. };
  336. var matter = createFnWithProps(matterFn, matterDict);
  337. var src_default = matter;
  338. export {
  339. src_default as default,
  340. engines_default as engines,
  341. matter,
  342. utils
  343. };