vitepress-openapi 0.2.15 → 0.2.17
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/{OAJSONEditor-BPdzinnW.js → OAJSONEditor-6uG2SFZK.js} +6 -6
- package/dist/{OAJSONEditor-cdYfH8pW.cjs → OAJSONEditor-D-Yhb6db.cjs} +4 -4
- package/dist/{angular-html-D5QwJSjx.js → angular-html-BUsdri4u.js} +2 -2
- package/dist/angular-html-BzD9NQW5.cjs +1 -0
- package/dist/{angular-ts-CXuTnAMH.cjs → angular-ts-D1fX05LE.cjs} +1 -1
- package/dist/{angular-ts-B0kZpGGd.js → angular-ts-DLzsnsUB.js} +2 -2
- package/dist/{apl-CjAWNKej.cjs → apl-Bo4qJ495.cjs} +1 -1
- package/dist/{apl-CO2hi6pu.js → apl-D9bD1gNv.js} +5 -5
- package/dist/{astro-C8AbPvmY.js → astro-D8q6Gy7P.js} +5 -5
- package/dist/{astro-C6WzLepe.cjs → astro-DCjm-pbh.cjs} +1 -1
- package/dist/{blade-bJOeoSe0.cjs → blade-B6fombNo.cjs} +1 -1
- package/dist/{blade-B3TXqIqP.js → blade-Sc601tfi.js} +7 -7
- package/dist/{c-BTzN4Ylc.js → c-CfqZXQ2T.js} +1 -1
- package/dist/c-Dn__oIvS.cjs +1 -0
- package/dist/{chapel-D3SOeXgF.cjs → chapel-D7VyqyYt.cjs} +1 -1
- package/dist/{chapel-DqUUkRYq.js → chapel-xSzmwfXQ.js} +1 -1
- package/dist/{cobol-dlQjEakv.cjs → cobol-CAKxSKZT.cjs} +1 -1
- package/dist/{cobol-DFLW8ca0.js → cobol-CHAgZDLS.js} +2 -2
- package/dist/{coffee-1KoIHtzW.js → coffee-CeAuLo4O.js} +1 -1
- package/dist/{coffee-TUzF5uor.cjs → coffee-JnCJAydr.cjs} +1 -1
- package/dist/{core-x4pNOQS7.cjs → core-CNg5cW98.cjs} +3 -3
- package/dist/{core-CSILRXKH.js → core-s1UIPu_z.js} +114 -114
- package/dist/cpp-1aC9AELp.cjs +1 -0
- package/dist/{cpp-yEMsPd-1.js → cpp-DAEET6ev.js} +3 -3
- package/dist/{crystal-D_nHX3jS.cjs → crystal-BfArRaXr.cjs} +1 -1
- package/dist/{crystal-CENIitzj.js → crystal-C1qz7UnE.js} +6 -6
- package/dist/css-BVuZPxTF.cjs +1 -0
- package/dist/{css-B-WLUKlS.js → css-BZAFXxqD.js} +1 -1
- package/dist/{dist-BxzxjMCz.cjs → dist-CpZb0vTN.cjs} +5 -5
- package/dist/{dist-C1v4Xm4x.js → dist-DV8PoRsD.js} +1 -1
- package/dist/{edge-BEj_2N8x.js → edge-CWZ5MU2M.js} +3 -3
- package/dist/{edge-f2PDRzgK.cjs → edge-JzsrLjb_.cjs} +1 -1
- package/dist/{elixir-CQkxmVTM.js → elixir-Crtqxz1W.js} +1 -1
- package/dist/{elixir-CIcaqRsY.cjs → elixir-QzvqYFsV.cjs} +1 -1
- package/dist/{elm-DzGi_nR3.js → elm-9rlZJ0du.js} +1 -1
- package/dist/{elm-7G0I-hE6.cjs → elm-DTTDc-PO.cjs} +1 -1
- package/dist/{engine-javascript-DSMQf2z2.js → engine-javascript-CoT6aZfI.js} +2 -2
- package/dist/{engine-javascript-DsJlQOsE.cjs → engine-javascript-DWPOYZRs.cjs} +2 -2
- package/dist/{erb-COP2UPZ7.js → erb-C6UwauYe.js} +2 -2
- package/dist/{erb-B9-cGaTm.cjs → erb-DPWXQYaP.cjs} +1 -1
- package/dist/generate-BAgVJTNQ.js +103 -0
- package/dist/generate-BVoluROJ.cjs +1 -0
- package/dist/generateMarkdown-BYnrcWs0.js +692 -0
- package/dist/generateMarkdown-DyJdABwj.cjs +31 -0
- package/dist/{git-rebase-Ku61ihN2.cjs → git-rebase-Cwp0uWvv.cjs} +1 -1
- package/dist/{git-rebase-DTYLKCWV.js → git-rebase-xIgRvJ_q.js} +1 -1
- package/dist/{glimmer-js-D29AEl8t.js → glimmer-js-CNGdvMhA.js} +4 -4
- package/dist/{glimmer-js-izmglT1O.cjs → glimmer-js-Df_yNNoE.cjs} +1 -1
- package/dist/{glimmer-ts-DYj3qCHB.js → glimmer-ts-Dq4P7Mkf.js} +4 -4
- package/dist/{glimmer-ts-DJ1fakxz.cjs → glimmer-ts-DsAOmOGY.cjs} +1 -1
- package/dist/glsl-CjiF-xkH.cjs +1 -0
- package/dist/{glsl-Bc6nFncG.js → glsl-DZfvb15l.js} +2 -2
- package/dist/{graphql-Ds5Ubte9.js → graphql-CZmb-US9.js} +5 -5
- package/dist/graphql-r7AX53Pp.cjs +1 -0
- package/dist/{hack-DlUSG6ek.cjs → hack-Cs2ncrxq.cjs} +1 -1
- package/dist/{hack-B6E-ISVN.js → hack-DIIRgI4h.js} +2 -2
- package/dist/haml-3bvHwbYf.cjs +1 -0
- package/dist/{haml-DSK5muqV.js → haml-aboBnjLl.js} +3 -3
- package/dist/{handlebars-BxmkFTU3.cjs → handlebars-C7PZ80pQ.cjs} +1 -1
- package/dist/{handlebars-CGjGomme.js → handlebars-Z3oHMxHt.js} +4 -4
- package/dist/html-CtaiB3Oi.cjs +1 -0
- package/dist/{html-DvKVaMAh.js → html-Cz0Xqdyq.js} +3 -3
- package/dist/{html-derivative-Due6nHaa.cjs → html-derivative-BYOQo3PG.cjs} +1 -1
- package/dist/{html-derivative--Yb6fTDM.js → html-derivative-BjrHnqif.js} +1 -1
- package/dist/{http-BDb2okFn.cjs → http-BA9nOlAq.cjs} +1 -1
- package/dist/{http-Wf5dll9t.js → http-ieHS9PeS.js} +4 -4
- package/dist/{hurl-Ds566NU_.cjs → hurl-DFGEnvSS.cjs} +1 -1
- package/dist/{hurl-1Afa28rK.js → hurl-Dg9NkXrC.js} +2 -2
- package/dist/{java-DMU2lkyP.js → java-DCXCvNHA.js} +1 -1
- package/dist/java-DTzZZzv7.cjs +1 -0
- package/dist/javascript-DOKgRqe8.cjs +1 -0
- package/dist/{javascript-CkOfli2O.js → javascript-f9cb-Pl7.js} +1 -1
- package/dist/{jinja-C_EeM5kX.js → jinja-B2DAjJOY.js} +1 -1
- package/dist/{jinja-D65--viX.cjs → jinja-DE8g1bjX.cjs} +1 -1
- package/dist/{jison-D5yGxh57.js → jison-5BvvygJS.js} +1 -1
- package/dist/{jison-CgJDwgW4.cjs → jison-BCQciWJO.cjs} +1 -1
- package/dist/js-yaml-B-Up3zwX.cjs +28 -0
- package/dist/{js-yaml-CA-Iojzo.js → js-yaml-o4oD1Gp3.js} +1 -1
- package/dist/json-BcxNDt_H.cjs +1 -0
- package/dist/{json-DnSlp2pP.js → json-DZfYi9f2.js} +1 -1
- package/dist/{jsx-DIHyTcyM.js → jsx-D0ksVAAA.js} +1 -1
- package/dist/jsx-DxOBIFYM.cjs +1 -0
- package/dist/{julia-Upv7yuF3.cjs → julia-B3y7weJz.cjs} +1 -1
- package/dist/{julia-pnYWyN4H.js → julia-Dk4vCuW5.js} +4 -4
- package/dist/{just-kcvRoQaC.cjs → just-B7ftAo6t.cjs} +1 -1
- package/dist/{just-Rux3ihaI.js → just-BDbjiXnk.js} +5 -5
- package/dist/{latex-BNmmGRr9.js → latex-CC_8gzv9.js} +1 -1
- package/dist/{latex-ssVj4acX.cjs → latex-QeOQCy8M.cjs} +1 -1
- package/dist/{liquid-D9-xfAH9.cjs → liquid-BVZIda1i.cjs} +1 -1
- package/dist/{liquid-CkFTSuz5.js → liquid-CJVpPXmh.js} +4 -4
- package/dist/llms-DWks2vGp.cjs +26 -0
- package/dist/llms-yFQ0insM.js +418 -0
- package/dist/lua-BoAqNcbB.cjs +1 -0
- package/dist/{lua-BPZzb4jg.js → lua-Bwwgpolt.js} +2 -2
- package/dist/{marko-CckGih8c.cjs → marko-CX9I4GhY.cjs} +1 -1
- package/dist/{marko-U5lgzX4W.js → marko-z-UpeqN9.js} +3 -3
- package/dist/{mdc-ftW0jKss.js → mdc-ANp6h8jQ.js} +2 -2
- package/dist/{mdc-CIgbmpvb.cjs → mdc-BPd7ZDIZ.cjs} +1 -1
- package/dist/{nginx-CO6FbhYm.js → nginx-BUK9HsFk.js} +1 -1
- package/dist/{nginx-pp3BriFA.cjs → nginx-CLECRmW5.cjs} +1 -1
- package/dist/{nim-xNROytAe.cjs → nim-4G662efB.cjs} +1 -1
- package/dist/{nim-CJpN9IuJ.js → nim-pupbS7ww.js} +6 -6
- package/dist/{useOpenapi-DVLwqpMR.js → normalizeExamples-BFR42ENV.js} +4058 -2185
- package/dist/normalizeExamples-DNaOGXm0.cjs +45 -0
- package/dist/{org-BLgfq4oa.js → org-B8vjAQ9v.js} +26 -26
- package/dist/{org-BfvkBzjQ.cjs → org-XYh7PPQk.cjs} +1 -1
- package/dist/{perl-4iy1AzjP.js → perl-ChiZYWB8.js} +5 -5
- package/dist/{perl-gYxlBF7Z.cjs → perl-Qi4toP6W.cjs} +1 -1
- package/dist/{php-D7tWq26a.cjs → php-93axekrF.cjs} +1 -1
- package/dist/{php-DBpFECnW.js → php-JTDYSOFN.js} +6 -6
- package/dist/{pug-CYLX2lIo.cjs → pug-BHWWe-s7.cjs} +1 -1
- package/dist/{pug-B3SC-VJn.js → pug-BR1NlFnA.js} +3 -3
- package/dist/{qml-CjtYDlYC.cjs → qml-71TJR8lk.cjs} +1 -1
- package/dist/{qml-BJryVNLB.js → qml-BBCxB4Da.js} +1 -1
- package/dist/r-BVapaf2o.cjs +1 -0
- package/dist/{r-CO3AGhxE.js → r-u-VAz7bR.js} +1 -1
- package/dist/{razor-fizwqQUn.js → razor-Cqi5RzG6.js} +1 -1
- package/dist/{razor-Cjwm6SbM.cjs → razor-DMdT60zm.cjs} +1 -1
- package/dist/{regexp-BlBS23LV.js → regexp-9Pkh3Nf7.js} +1 -1
- package/dist/regexp-CwHm4hi_.cjs +1 -0
- package/dist/{rst-uzixnQEN.cjs → rst-BtwmHjj9.cjs} +1 -1
- package/dist/{rst-CQ-BOew2.js → rst-CLZRH5oU.js} +6 -6
- package/dist/{ruby-DYscwpKq.js → ruby-BZUQEFGQ.js} +12 -12
- package/dist/{ruby-DcF3FpOo.cjs → ruby-DuGe7Fs0.cjs} +1 -1
- package/dist/{sas-CQkHM7U4.cjs → sas-B7hIWqYI.cjs} +1 -1
- package/dist/{sas-xf7cYJ8D.js → sas-BO2zDPuE.js} +1 -1
- package/dist/scss-BpK0m8eZ.cjs +1 -0
- package/dist/{scss-D5Ro0FPj.js → scss-OFCrSwaR.js} +2 -2
- package/dist/{shellscript-DVhVbKuT.js → shellscript-BNTFxSmg.js} +1 -1
- package/dist/shellscript-KJDkFDLp.cjs +1 -0
- package/dist/{shellsession-DCV0F4Wc.js → shellsession-B5j5FuGA.js} +1 -1
- package/dist/{shellsession-CfBYvvpU.cjs → shellsession-DWalcvtJ.cjs} +1 -1
- package/dist/{soy-HCmyBkD1.cjs → soy-BQn3ItqC.cjs} +1 -1
- package/dist/{soy-DqvPS_mm.js → soy-C2Q794LP.js} +1 -1
- package/dist/{sql-C8Kl76rR.js → sql-8JHRpHTE.js} +1 -1
- package/dist/sql-C5y5nUuw.cjs +1 -0
- package/dist/{stata-CJiPjcNk.js → stata-BrNyU7bS.js} +1 -1
- package/dist/{stata-B1WKRY4L.cjs → stata-DyBO95gA.cjs} +1 -1
- package/dist/{surrealql-D34WxIUj.cjs → surrealql-CAAIvFqO.cjs} +1 -1
- package/dist/{surrealql-1H6TjudE.js → surrealql-DgN6gei3.js} +1 -1
- package/dist/{svelte-BdqWwltV.cjs → svelte-DGjw_UH1.cjs} +1 -1
- package/dist/{svelte-CIN2FBHe.js → svelte-FhQBX-tv.js} +3 -3
- package/dist/{templ-DfCxDv8S.js → templ-BUggPx5p.js} +2 -2
- package/dist/{templ-DRfIS7Yk.cjs → templ-Cxuj5rzV.cjs} +1 -1
- package/dist/tex-B3aY5DbH.cjs +1 -0
- package/dist/{tex-BTjGzapI.js → tex-BlOdu2Xy.js} +2 -2
- package/dist/{ts-tags-T_iDSaWH.cjs → ts-tags-BymwG1F9.cjs} +1 -1
- package/dist/{ts-tags-CV7DYDTm.js → ts-tags-DZBZzNkR.js} +7 -7
- package/dist/tsx-DgA8B92F.cjs +1 -0
- package/dist/{tsx-C9xVNnaW.js → tsx-mByJmMSt.js} +1 -1
- package/dist/{twig-C4i_cRPo.js → twig--2v8RtVi.js} +5 -5
- package/dist/{twig-D914CUNQ.cjs → twig-CWwcCfHy.cjs} +1 -1
- package/dist/types/src/cli.d.ts +1 -0
- package/dist/types/src/components/Common/Highlight/OAShiki.vue.d.ts +2 -2
- package/dist/types/src/components/Common/Highlight/OAVueJsonPretty.vue.d.ts +2 -2
- package/dist/types/src/components/Common/OACodeBlock.vue.d.ts +2 -2
- package/dist/types/src/components/Common/OACodeValue.vue.d.ts +2 -2
- package/dist/types/src/components/Common/OAJSONEditor.vue.d.ts +2 -2
- package/dist/types/src/components/Parameter/OAParameterAttribute.vue.d.ts +2 -2
- package/dist/types/src/components/Playground/OAPlaygroundParameterInput.vue.d.ts +2 -2
- package/dist/types/src/components/ui/badge/index.d.ts +1 -1
- package/dist/types/src/components/ui/button/index.d.ts +1 -1
- package/dist/types/src/composables/useTheme.d.ts +7 -0
- package/dist/types/src/index.d.ts +18 -3
- package/dist/types/src/lib/codeSamples/buildOperationRequest.d.ts +15 -0
- package/dist/types/src/lib/codeSamples/buildRequest.d.ts +6 -0
- package/dist/types/src/lib/constants.d.ts +3 -0
- package/dist/types/src/lib/markdown/generateMarkdown.d.ts +111 -0
- package/dist/types/src/lib/markdown/llmsSite.d.ts +137 -0
- package/dist/types/src/lib/markdown/markdownUtils.d.ts +35 -0
- package/dist/types/src/lib/markdown/operationToMarkdown.d.ts +66 -0
- package/dist/types/src/lib/markdown/sanitizeHtml.d.ts +5 -0
- package/dist/types/src/lib/parser/getSchemaUi.d.ts +1 -0
- package/dist/types/src/node/cli.d.ts +5 -0
- package/dist/types/src/node/generate.d.ts +38 -0
- package/dist/types/src/node/llms.d.ts +99 -0
- package/dist/types/src/node/llmsPlugin.d.ts +10 -0
- package/dist/typescript-BWfpZQ2O.cjs +1 -0
- package/dist/{typescript-Chl77Ui1.js → typescript-DF15DAwn.js} +1 -1
- package/dist/{typst-C6fVPKNq.cjs → typst-BEqsnZtv.cjs} +1 -1
- package/dist/{typst-BgQE6TAB.js → typst-uhRd3eU5.js} +29 -29
- package/dist/useSidebar-B2LH297o.cjs +6 -0
- package/dist/useSidebar-CoebjdWM.js +313 -0
- package/dist/vitepress-openapi.cli.cjs.js +22 -0
- package/dist/vitepress-openapi.cli.es.js +67 -0
- package/dist/vitepress-openapi.client.cjs.js +1 -1
- package/dist/vitepress-openapi.client.es.js +7 -6
- package/dist/vitepress-openapi.css +1 -1
- package/dist/vitepress-openapi.node.cjs.js +1 -1
- package/dist/vitepress-openapi.node.es.js +47 -2
- package/dist/vitepress-openapi.vue.cjs.js +1 -1
- package/dist/vitepress-openapi.vue.es.js +4 -3
- package/dist/{vue-CaajMfVy.cjs → vue-BQl-5Fd6.cjs} +1 -1
- package/dist/{vue-J95qJkfJ.js → vue-CSJXDMn1.js} +6 -6
- package/dist/{vue-DpVGGK22.js → vue-DIM9jH1x.js} +3091 -4626
- package/dist/vue-DwsEa2e4.cjs +7 -0
- package/dist/{vue-html-CtmhMKZ8.cjs → vue-html-AqZQgz92.cjs} +1 -1
- package/dist/{vue-html-BjrCwjva.js → vue-html-DYm4V3uo.js} +1 -1
- package/dist/{vue-vine-C4hFAX9j.js → vue-vine-BLrCVeql.js} +3 -3
- package/dist/{vue-vine-DHwm8nhC.cjs → vue-vine-C1QCIUv6.cjs} +1 -1
- package/dist/xml-DbN80t5u.cjs +1 -0
- package/dist/{xml-4tT4oeZd.js → xml-Df1jLF7H.js} +2 -2
- package/dist/{xsl-gK25ns76.cjs → xsl-7fKL5NbN.cjs} +1 -1
- package/dist/{xsl-ZTs2D31w.js → xsl-C2LJLnIq.js} +1 -1
- package/dist/{yaml-Cr99duBz.js → yaml-DABI1u00.js} +1 -1
- package/dist/yaml-DNB0t8B9.cjs +1 -0
- package/package.json +6 -2
- package/src/cli.ts +6 -0
- package/src/composables/useMarkdown.ts +8 -2
- package/src/composables/useSidebar.ts +1 -1
- package/src/composables/useTheme.ts +12 -0
- package/src/index.ts +26 -5
- package/src/lib/codeSamples/buildHarRequest.ts +5 -0
- package/src/lib/codeSamples/buildOperationRequest.ts +52 -0
- package/src/lib/codeSamples/buildRequest.ts +49 -28
- package/src/lib/constants.ts +5 -0
- package/src/lib/examples/getSchemaUiJson.ts +1 -1
- package/src/lib/markdown/generateMarkdown.ts +451 -0
- package/src/lib/markdown/llmsSite.ts +475 -0
- package/src/lib/markdown/markdownUtils.ts +104 -0
- package/src/lib/markdown/operationLink.ts +10 -3
- package/src/lib/markdown/operationToMarkdown.ts +834 -0
- package/src/lib/markdown/sanitizeHtml.ts +92 -0
- package/src/lib/parser/generateCodeSamples.ts +6 -31
- package/src/lib/parser/generateMissingOperationIds.ts +1 -1
- package/src/lib/parser/generateMissingSummary.ts +1 -1
- package/src/lib/parser/generateMissingTags.ts +1 -1
- package/src/lib/parser/generateRequestBodyUi.ts +1 -1
- package/src/lib/parser/generateResponseUi.ts +1 -1
- package/src/lib/parser/generateSecurityUi.ts +1 -1
- package/src/lib/parser/getSchemaUi.ts +7 -3
- package/src/lib/sidebar/generateSidebarItemsByPaths.ts +1 -1
- package/src/lib/spec/createOpenApiSpec.ts +1 -1
- package/src/node/cli.ts +107 -0
- package/src/node/generate.ts +207 -0
- package/src/node/llms.ts +431 -0
- package/src/node/llmsPlugin.ts +29 -0
- package/dist/angular-html-BoKsRe8T.cjs +0 -1
- package/dist/c-BCyoZ90X.cjs +0 -1
- package/dist/cpp-tnOfy09q.cjs +0 -1
- package/dist/css-02TmpFbt.cjs +0 -1
- package/dist/glsl-BGTPqLyQ.cjs +0 -1
- package/dist/graphql-DdYH_Rp2.cjs +0 -1
- package/dist/haml-D-xAsnlc.cjs +0 -1
- package/dist/html-BYiFDapk.cjs +0 -1
- package/dist/java-DwnayQnb.cjs +0 -1
- package/dist/javascript-CSKossUl.cjs +0 -1
- package/dist/js-yaml-uHYMr7LM.cjs +0 -28
- package/dist/json-DMvxal4O.cjs +0 -1
- package/dist/jsx-CJAVXoYP.cjs +0 -1
- package/dist/lua-CbILlNRp.cjs +0 -1
- package/dist/r-DKRgTYMs.cjs +0 -1
- package/dist/regexp-BbrbtHcV.cjs +0 -1
- package/dist/scss-B6SEboBH.cjs +0 -1
- package/dist/shellscript-UM_LESkp.cjs +0 -1
- package/dist/sql-B2O4UoSw.cjs +0 -1
- package/dist/tex-W_Dxzamg.cjs +0 -1
- package/dist/tsx-B3iFrpuZ.cjs +0 -1
- package/dist/typescript-Ai8puZIn.cjs +0 -1
- package/dist/useOpenapi-D3rJQzeG.cjs +0 -50
- package/dist/vue-Cdo23EVh.cjs +0 -6
- package/dist/xml-C1x13bX0.cjs +0 -1
- package/dist/yaml-BUexFoMb.cjs +0 -1
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type * as XSS from 'xss'
|
|
2
|
+
import type { IWhiteList } from 'xss'
|
|
3
|
+
import xssModule from 'xss'
|
|
4
|
+
|
|
5
|
+
// `xss` is a CommonJS module: its helpers are only reliably reachable through the default export.
|
|
6
|
+
const {
|
|
7
|
+
escapeAttrValue,
|
|
8
|
+
FilterXSS,
|
|
9
|
+
friendlyAttrValue,
|
|
10
|
+
getDefaultWhiteList,
|
|
11
|
+
safeAttrValue,
|
|
12
|
+
} = xssModule as unknown as typeof XSS
|
|
13
|
+
|
|
14
|
+
/** Attributes allowed on every allowed tag. */
|
|
15
|
+
const GLOBAL_ATTRIBUTES = ['class', 'id', 'title', 'align', 'target', 'rel', 'style']
|
|
16
|
+
|
|
17
|
+
/** Tags that markdown-it (and common plugins) can emit, missing from the js-xss default whitelist. */
|
|
18
|
+
const EXTRA_TAGS: IWhiteList = {
|
|
19
|
+
input: ['type', 'checked', 'disabled'],
|
|
20
|
+
q: ['cite'],
|
|
21
|
+
dfn: [],
|
|
22
|
+
var: [],
|
|
23
|
+
samp: [],
|
|
24
|
+
wbr: [],
|
|
25
|
+
time: ['datetime'],
|
|
26
|
+
ruby: [],
|
|
27
|
+
rt: [],
|
|
28
|
+
rp: [],
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Extra per-tag attributes markdown-it emits. */
|
|
32
|
+
const EXTRA_TAG_ATTRIBUTES: IWhiteList = {
|
|
33
|
+
ol: ['start', 'type', 'reversed'],
|
|
34
|
+
li: ['value'],
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const RE_DATA_OR_ARIA_ATTRIBUTE = /^(?:data|aria)-[\w-]+$/
|
|
38
|
+
// Control characters and whitespace, ignored by browsers when parsing URLs.
|
|
39
|
+
const RE_URL_IGNORED_CHARS = /[\s\p{Cc}]/gu
|
|
40
|
+
// A URL has a scheme when a colon appears before the first `/`, `?` or `#`.
|
|
41
|
+
const RE_URL_SCHEME = /^[^/?#]*:/
|
|
42
|
+
|
|
43
|
+
function buildWhiteList(): IWhiteList {
|
|
44
|
+
const whiteList: IWhiteList = { ...getDefaultWhiteList(), ...EXTRA_TAGS }
|
|
45
|
+
|
|
46
|
+
for (const [tag, attributes] of Object.entries(EXTRA_TAG_ATTRIBUTES)) {
|
|
47
|
+
whiteList[tag] = [...(whiteList[tag] ?? []), ...(attributes ?? [])]
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
for (const tag of Object.keys(whiteList)) {
|
|
51
|
+
whiteList[tag] = [...new Set([...(whiteList[tag] ?? []), ...GLOBAL_ATTRIBUTES])]
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return whiteList
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const filter = new FilterXSS({
|
|
58
|
+
whiteList: buildWhiteList(),
|
|
59
|
+
// Remove tags outside the whitelist (keeping their text), and drop the content of these entirely.
|
|
60
|
+
stripIgnoreTag: true,
|
|
61
|
+
stripIgnoreTagBody: ['script', 'style'],
|
|
62
|
+
allowCommentTag: false,
|
|
63
|
+
onIgnoreTagAttr(_tag, name, value) {
|
|
64
|
+
if (RE_DATA_OR_ARIA_ATTRIBUTE.test(name)) {
|
|
65
|
+
return `${name}="${escapeAttrValue(value)}"`
|
|
66
|
+
}
|
|
67
|
+
// Any other attribute (including `on*` event handlers) is removed.
|
|
68
|
+
return undefined
|
|
69
|
+
},
|
|
70
|
+
safeAttrValue(tag, name, value, cssFilter) {
|
|
71
|
+
const safeValue = safeAttrValue(tag, name, value, cssFilter)
|
|
72
|
+
|
|
73
|
+
// js-xss only keeps absolute URLs from a fixed list of schemes and `/`, `./`, `../`, `#` prefixes.
|
|
74
|
+
// Also keep scheme-less relative URLs (e.g. `users/list`, `?page=2`) emitted by Markdown links.
|
|
75
|
+
if ((name === 'href' || name === 'src') && safeValue === '') {
|
|
76
|
+
const url = friendlyAttrValue(value).trim()
|
|
77
|
+
if (url !== '' && !RE_URL_SCHEME.test(url.replace(RE_URL_IGNORED_CHARS, ''))) {
|
|
78
|
+
return escapeAttrValue(url)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return safeValue
|
|
83
|
+
},
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Sanitizes HTML rendered from Markdown: keeps formatting markup (headings, lists, tables, code,
|
|
88
|
+
* links, images, `<details>`, …) and removes scripts, event handlers and unsafe URLs.
|
|
89
|
+
*/
|
|
90
|
+
export function sanitizeHtml(html: string): string {
|
|
91
|
+
return filter.process(html)
|
|
92
|
+
}
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { OAExampleObject, ParsedOpenAPI, ParsedOperation, PlaygroundSecurityScheme } from '../../types'
|
|
1
|
+
import type { ParsedOpenAPI, ParsedOperation } from '../../types'
|
|
3
2
|
import { availableLanguages, useTheme } from '../../composables/useTheme'
|
|
4
|
-
import {
|
|
5
|
-
import { buildRequest } from '../codeSamples/buildRequest'
|
|
3
|
+
import { buildOperationRequest } from '../codeSamples/buildOperationRequest'
|
|
6
4
|
import { generateCodeSample } from '../codeSamples/generateCodeSample'
|
|
5
|
+
import { httpVerbs } from '../constants'
|
|
7
6
|
import { resolveBaseUrl } from '../utils/resolveBaseUrl'
|
|
8
7
|
|
|
9
8
|
export async function generateCodeSamples(spec: ParsedOpenAPI): Promise<ParsedOpenAPI> {
|
|
@@ -21,36 +20,12 @@ export async function generateCodeSamples(spec: ParsedOpenAPI): Promise<ParsedOp
|
|
|
21
20
|
continue
|
|
22
21
|
}
|
|
23
22
|
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
const requestBodyContentTypes = Object.keys(operation.requestBody?.content || {})
|
|
27
|
-
const requestBodyExamples: OAExampleObject[] = requestBodyContentTypes
|
|
28
|
-
.map((contentType) => {
|
|
29
|
-
return Object.values(operation.requestBody?.content?.[contentType]?.examples || {})
|
|
30
|
-
})
|
|
31
|
-
.flat()
|
|
32
|
-
|
|
33
|
-
const autogeneratedRequestBodyExample = requestBodyExamples
|
|
34
|
-
.find(example => example?.isAutogenerated)
|
|
35
|
-
|
|
36
|
-
const request = buildRequest({
|
|
23
|
+
const request = buildOperationRequest({
|
|
37
24
|
path,
|
|
38
25
|
method: verb,
|
|
26
|
+
operation,
|
|
39
27
|
baseUrl,
|
|
40
|
-
|
|
41
|
-
authorizations: Object.entries(authorizations).map(([name, value]: [string, any]) => {
|
|
42
|
-
return {
|
|
43
|
-
...value,
|
|
44
|
-
name: value.name ?? String(name),
|
|
45
|
-
label: String(name),
|
|
46
|
-
} as PlaygroundSecurityScheme
|
|
47
|
-
}),
|
|
48
|
-
body: autogeneratedRequestBodyExample?.value || undefined,
|
|
49
|
-
headers: {
|
|
50
|
-
...(useTheme().getCodeSamplesDefaultHeaders() || {}),
|
|
51
|
-
},
|
|
52
|
-
variables: {},
|
|
53
|
-
contentType: requestBodyContentTypes.length > 0 ? requestBodyContentTypes[0] : undefined,
|
|
28
|
+
headers: useTheme().getCodeSamplesDefaultHeaders() || {},
|
|
54
29
|
})
|
|
55
30
|
|
|
56
31
|
operation.codeSamples = await Promise.all(
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ParsedContent, ParsedOpenAPI, ParsedOperation } from '../../types'
|
|
2
|
-
import { httpVerbs } from '
|
|
2
|
+
import { httpVerbs } from '../constants'
|
|
3
3
|
import { getSchemaExample } from '../examples/getSchemaExample'
|
|
4
4
|
import { getSchemaUi, SchemaUiCache } from './getSchemaUi'
|
|
5
5
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ParsedContent, ParsedOpenAPI, ParsedOperation } from '../../types'
|
|
2
|
-
import { httpVerbs } from '
|
|
2
|
+
import { httpVerbs } from '../constants'
|
|
3
3
|
import { getSchemaExample } from '../examples/getSchemaExample'
|
|
4
4
|
import { getSchemaUi, SchemaUiCache } from './getSchemaUi'
|
|
5
5
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ParsedOpenAPI, ParsedOperation } from '../../types'
|
|
2
|
-
import { httpVerbs } from '
|
|
2
|
+
import { httpVerbs } from '../constants'
|
|
3
3
|
import { getSecurityUi } from './getSecurityUi'
|
|
4
4
|
|
|
5
5
|
export function generateSecurityUi(spec: ParsedOpenAPI): ParsedOpenAPI {
|
|
@@ -50,10 +50,12 @@ export interface OAProperty {
|
|
|
50
50
|
export class SchemaUiCache {
|
|
51
51
|
walked = new WeakSet<object>()
|
|
52
52
|
ui = new WeakMap<object, OAProperty>()
|
|
53
|
+
// Schema each returned property was built from (a side table, so properties stay plain data).
|
|
54
|
+
sources = new WeakMap<OAProperty, object>()
|
|
53
55
|
}
|
|
54
56
|
|
|
55
57
|
class UiPropertyFactory {
|
|
56
|
-
constructor(private memo: WeakMap<object, OAProperty>) {}
|
|
58
|
+
constructor(private memo: WeakMap<object, OAProperty>, private sources?: WeakMap<OAProperty, object>) {}
|
|
57
59
|
|
|
58
60
|
static createBaseProperty(
|
|
59
61
|
name: string,
|
|
@@ -187,7 +189,9 @@ class UiPropertyFactory {
|
|
|
187
189
|
}
|
|
188
190
|
|
|
189
191
|
// Shallow copy: callers reassign `meta`/`required` on the returned object.
|
|
190
|
-
|
|
192
|
+
const copy = { ...property, name, required }
|
|
193
|
+
this.sources?.set(copy, schema)
|
|
194
|
+
return copy
|
|
191
195
|
}
|
|
192
196
|
|
|
193
197
|
buildUiProperty(
|
|
@@ -416,7 +420,7 @@ export function getSchemaUi(
|
|
|
416
420
|
|
|
417
421
|
const resolvedSchema = resolveCircularRef(jsonSchema, cache.walked)
|
|
418
422
|
|
|
419
|
-
return new UiPropertyFactory(cache.ui).schemaToUiProperty('', resolvedSchema)
|
|
423
|
+
return new UiPropertyFactory(cache.ui, cache.sources).schemaToUiProperty('', resolvedSchema)
|
|
420
424
|
}
|
|
421
425
|
|
|
422
426
|
function inferTypesFromEnum(values: unknown[]): JSONSchemaType[] {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { OpenAPIV3 } from '@scalar/openapi-types'
|
|
2
2
|
import type { OASidebarItem, SidebarGroupTemplateFn, SidebarItemTemplateFn } from '../../composables/useSidebar'
|
|
3
|
-
import { httpVerbs } from '
|
|
3
|
+
import { httpVerbs } from '../constants'
|
|
4
4
|
import { ensureGroupTextSlashPrefix } from './ensureGroupTextSlashPrefix'
|
|
5
5
|
import { flatSidebarItems } from './flatSidebarItems'
|
|
6
6
|
import { mergeAdjacentGroups } from './mergeAdjacentGroups'
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { OpenAPIV3 } from '@scalar/openapi-types'
|
|
2
2
|
import type { OpenAPIDocument, ParsedOpenAPI, ParsedPaths } from '../../types'
|
|
3
|
-
import { httpVerbs } from '
|
|
3
|
+
import { httpVerbs } from '../constants'
|
|
4
4
|
|
|
5
5
|
export interface OpenApiSpecInstance {
|
|
6
6
|
spec: OpenAPIDocument
|
package/src/node/cli.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { GenerateMarkdownOptions } from './generate'
|
|
2
|
+
import { parseArgs } from 'node:util'
|
|
3
|
+
import { generateMarkdown } from './generate'
|
|
4
|
+
|
|
5
|
+
const HELP = `Usage: vitepress-openapi generate <spec> --out <dir> [options]
|
|
6
|
+
|
|
7
|
+
Generate plain Markdown (and llms.txt) from an OpenAPI 3.x document.
|
|
8
|
+
|
|
9
|
+
Arguments:
|
|
10
|
+
<spec> Path or URL to a JSON/YAML OpenAPI document
|
|
11
|
+
|
|
12
|
+
Options:
|
|
13
|
+
-o, --out <dir> Output directory (required)
|
|
14
|
+
--group-by <mode> "operation" (default) or "tag"
|
|
15
|
+
--link-prefix <prefix> Prefix of the links in llms.txt (default "/")
|
|
16
|
+
--openapi-url <url> URL of the OpenAPI document, linked from every page
|
|
17
|
+
--code-samples <langs> Comma-separated languages: curl, javascript, php, python (default "curl")
|
|
18
|
+
--no-code-samples Do not include code samples
|
|
19
|
+
--base-url <url> Base URL used in code samples (default: first server)
|
|
20
|
+
--no-llms Do not generate llms.txt and llms-full.txt
|
|
21
|
+
--no-frontmatter Do not add VitePress frontmatter
|
|
22
|
+
--clean Remove the output directory before writing
|
|
23
|
+
-h, --help Show this help
|
|
24
|
+
`
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Runs the CLI with the given arguments (without the node/binary path).
|
|
28
|
+
* Returns the process exit code.
|
|
29
|
+
*/
|
|
30
|
+
export async function runCli(args: string[]): Promise<number> {
|
|
31
|
+
let parsed
|
|
32
|
+
try {
|
|
33
|
+
parsed = parseArgs({
|
|
34
|
+
args,
|
|
35
|
+
allowPositionals: true,
|
|
36
|
+
options: {
|
|
37
|
+
'out': { type: 'string', short: 'o' },
|
|
38
|
+
'group-by': { type: 'string' },
|
|
39
|
+
'link-prefix': { type: 'string' },
|
|
40
|
+
'openapi-url': { type: 'string' },
|
|
41
|
+
'code-samples': { type: 'string' },
|
|
42
|
+
'no-code-samples': { type: 'boolean' },
|
|
43
|
+
'base-url': { type: 'string' },
|
|
44
|
+
'no-llms': { type: 'boolean' },
|
|
45
|
+
'no-frontmatter': { type: 'boolean' },
|
|
46
|
+
'clean': { type: 'boolean' },
|
|
47
|
+
'help': { type: 'boolean', short: 'h' },
|
|
48
|
+
},
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
console.error((error as Error).message)
|
|
53
|
+
console.error(HELP)
|
|
54
|
+
return 1
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const { values, positionals } = parsed
|
|
58
|
+
const [command, spec] = positionals
|
|
59
|
+
|
|
60
|
+
if (values.help || !command) {
|
|
61
|
+
console.log(HELP)
|
|
62
|
+
return values.help ? 0 : 1
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (command !== 'generate') {
|
|
66
|
+
console.error(`Unknown command "${command}".\n`)
|
|
67
|
+
console.error(HELP)
|
|
68
|
+
return 1
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (!spec || !values.out) {
|
|
72
|
+
console.error('Missing <spec> or --out.\n')
|
|
73
|
+
console.error(HELP)
|
|
74
|
+
return 1
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const groupBy = values['group-by'] ?? 'operation'
|
|
78
|
+
if (groupBy !== 'operation' && groupBy !== 'tag') {
|
|
79
|
+
console.error(`Invalid --group-by "${groupBy}": use "operation" or "tag".`)
|
|
80
|
+
return 1
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const options: GenerateMarkdownOptions = {
|
|
84
|
+
...(/^https?:\/\//i.test(spec) ? { specUrl: spec } : { specPath: spec }),
|
|
85
|
+
outDir: values.out,
|
|
86
|
+
groupBy,
|
|
87
|
+
linkPrefix: values['link-prefix'],
|
|
88
|
+
openapiUrl: values['openapi-url'],
|
|
89
|
+
baseUrl: values['base-url'],
|
|
90
|
+
codeSamples: values['no-code-samples']
|
|
91
|
+
? false
|
|
92
|
+
: values['code-samples']?.split(',').map(lang => lang.trim()).filter(Boolean),
|
|
93
|
+
llms: !values['no-llms'],
|
|
94
|
+
frontmatter: !values['no-frontmatter'],
|
|
95
|
+
clean: values.clean ?? false,
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
try {
|
|
99
|
+
const written = await generateMarkdown(options)
|
|
100
|
+
console.log(`Generated ${written.length} files in ${options.outDir}`)
|
|
101
|
+
return 0
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
console.error((error as Error).message)
|
|
105
|
+
return 1
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import type { GenerateMarkdownFilesOptions, MarkdownFile } from '../lib/markdown/generateMarkdown'
|
|
2
|
+
import type { OpenAPIDocument } from '../types'
|
|
3
|
+
import { generateMarkdownFiles } from '../lib/markdown/generateMarkdown'
|
|
4
|
+
import { parseSpec } from '../lib/utils/parseSpec'
|
|
5
|
+
|
|
6
|
+
// The package root entry is also bundled for the browser (e.g. `useSidebar` in a theme
|
|
7
|
+
// component). Non-literal specifiers keep bundlers from resolving Node built-ins there.
|
|
8
|
+
function importNodeModule(specifier: string): Promise<any> {
|
|
9
|
+
return import(/* @vite-ignore */ specifier)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export async function loadNodeModules() {
|
|
13
|
+
const [fs, path, process] = await Promise.all([
|
|
14
|
+
importNodeModule('node:fs/promises') as Promise<typeof import('node:fs/promises')>,
|
|
15
|
+
importNodeModule('node:path') as Promise<typeof import('node:path')>,
|
|
16
|
+
importNodeModule('node:process').then(module => module.default) as Promise<typeof import('node:process')>,
|
|
17
|
+
])
|
|
18
|
+
return { fs, path, process }
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type NodeModules = Awaited<ReturnType<typeof loadNodeModules>>
|
|
22
|
+
|
|
23
|
+
export interface SpecSource {
|
|
24
|
+
/** OpenAPI document (object, or JSON/YAML string). */
|
|
25
|
+
spec?: OpenAPIDocument | string
|
|
26
|
+
/** Path to a local JSON/YAML OpenAPI document. */
|
|
27
|
+
specPath?: string
|
|
28
|
+
/** URL of a JSON/YAML OpenAPI document. */
|
|
29
|
+
specUrl?: string
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface GenerateMarkdownOptions extends GenerateMarkdownFilesOptions, SpecSource {
|
|
33
|
+
/** Output directory. Created if missing. */
|
|
34
|
+
outDir: string
|
|
35
|
+
/**
|
|
36
|
+
* Remove the output directory before writing.
|
|
37
|
+
* @default false
|
|
38
|
+
*/
|
|
39
|
+
clean?: boolean
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Loads an OpenAPI document, generates Markdown files (see `generateMarkdownFiles`)
|
|
44
|
+
* and writes them to `outDir`. Returns the absolute paths of the written files.
|
|
45
|
+
*/
|
|
46
|
+
export async function generateMarkdown(options: GenerateMarkdownOptions): Promise<string[]> {
|
|
47
|
+
const { spec, specPath, specUrl, outDir, clean = false, ...generateOptions } = options
|
|
48
|
+
|
|
49
|
+
if (!outDir) {
|
|
50
|
+
throw new Error('[vitepress-openapi] `outDir` is required.')
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const modules = await loadNodeModules()
|
|
54
|
+
const { fs, path, process } = modules
|
|
55
|
+
const { resolve } = path
|
|
56
|
+
|
|
57
|
+
const document = await loadSpec({ spec, specPath, specUrl }, modules)
|
|
58
|
+
const files = await generateMarkdownFiles(document, generateOptions)
|
|
59
|
+
const root = resolve(outDir)
|
|
60
|
+
|
|
61
|
+
// After `clean`, write through the checked canonical path: if `outDir` is a symlink,
|
|
62
|
+
// its target was removed and the link dangles until the directory is recreated.
|
|
63
|
+
let writeRoot = root
|
|
64
|
+
if (clean) {
|
|
65
|
+
// Compare canonical paths, so a symlink can't point `clean` at cwd or one of its parents.
|
|
66
|
+
writeRoot = await canonicalPath(root, fs, path)
|
|
67
|
+
assertSafeToClean(writeRoot, await fs.realpath(process.cwd()), path)
|
|
68
|
+
await fs.rm(writeRoot, { recursive: true, force: true })
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return writeFilesSafely(writeRoot, files, modules)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Writes `files` (paths relative to `root`) without leaving `root`: targets must stay inside it,
|
|
76
|
+
* parent directories must resolve inside it, and symlinked files are never followed.
|
|
77
|
+
* Returns the absolute paths of the written files.
|
|
78
|
+
*/
|
|
79
|
+
export async function writeFilesSafely(root: string, files: MarkdownFile[], { fs, path }: NodeModules): Promise<string[]> {
|
|
80
|
+
const { basename, dirname, join, resolve } = path
|
|
81
|
+
|
|
82
|
+
await fs.mkdir(root, { recursive: true })
|
|
83
|
+
const realRoot = await fs.realpath(root)
|
|
84
|
+
|
|
85
|
+
const written: string[] = []
|
|
86
|
+
for (const file of files) {
|
|
87
|
+
const target = resolve(root, file.path)
|
|
88
|
+
if (!isInside(root, target, path)) {
|
|
89
|
+
throw new Error(`[vitepress-openapi] Refusing to write outside of outDir: ${file.path}`)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Don't follow symlinks: parent directories must resolve inside outDir, and the file itself can't be a link.
|
|
93
|
+
await fs.mkdir(dirname(target), { recursive: true })
|
|
94
|
+
const realParent = await fs.realpath(dirname(target))
|
|
95
|
+
if (!isInside(realRoot, realParent, path)) {
|
|
96
|
+
throw new Error(`[vitepress-openapi] Refusing to write through a symlink outside of outDir: ${file.path}`)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
await writeFileNoFollow(join(realParent, basename(target)), file.content, file.path, fs)
|
|
100
|
+
written.push(target)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return written
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Writes a file without following a symlink at its path. `O_NOFOLLOW` makes the check
|
|
108
|
+
* atomic with the open; where it doesn't exist (Windows), fall back to `lstat`.
|
|
109
|
+
*/
|
|
110
|
+
async function writeFileNoFollow(
|
|
111
|
+
target: string,
|
|
112
|
+
content: string,
|
|
113
|
+
label: string,
|
|
114
|
+
fs: typeof import('node:fs/promises'),
|
|
115
|
+
): Promise<void> {
|
|
116
|
+
const symlinkError = () => new Error(`[vitepress-openapi] Refusing to overwrite a symlink: ${label}`)
|
|
117
|
+
const { O_CREAT, O_NOFOLLOW, O_TRUNC, O_WRONLY } = fs.constants
|
|
118
|
+
|
|
119
|
+
if (O_NOFOLLOW === undefined) {
|
|
120
|
+
const stats = await fs.lstat(target).catch(() => null)
|
|
121
|
+
if (stats?.isSymbolicLink()) {
|
|
122
|
+
throw symlinkError()
|
|
123
|
+
}
|
|
124
|
+
await fs.writeFile(target, content, 'utf-8')
|
|
125
|
+
return
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
let handle: Awaited<ReturnType<typeof fs.open>>
|
|
129
|
+
try {
|
|
130
|
+
handle = await fs.open(target, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW)
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
throw (error as NodeJS.ErrnoException).code === 'ELOOP' ? symlinkError() : error
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
try {
|
|
137
|
+
await handle.writeFile(content, 'utf-8')
|
|
138
|
+
}
|
|
139
|
+
finally {
|
|
140
|
+
await handle.close()
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export async function loadSpec({ spec, specPath, specUrl }: SpecSource, { fs, path }: NodeModules): Promise<OpenAPIDocument> {
|
|
145
|
+
if (spec) {
|
|
146
|
+
return parseSpec(spec)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (specPath) {
|
|
150
|
+
return parseSpec(await fs.readFile(path.resolve(specPath), 'utf-8'))
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (specUrl) {
|
|
154
|
+
const response = await fetch(specUrl)
|
|
155
|
+
if (!response.ok) {
|
|
156
|
+
throw new Error(`[vitepress-openapi] Could not fetch ${specUrl}: ${response.status} ${response.statusText}`)
|
|
157
|
+
}
|
|
158
|
+
return parseSpec(await response.text())
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
throw new Error('[vitepress-openapi] One of `spec`, `specPath` or `specUrl` is required.')
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Whether `child` is `parent` or inside it. Only `..` or `../…` count as upward,
|
|
166
|
+
* so a directory named `..project` is still inside.
|
|
167
|
+
*/
|
|
168
|
+
function isInside(parent: string, child: string, { isAbsolute, relative, sep }: typeof import('node:path')): boolean {
|
|
169
|
+
const fromParent = relative(parent, child)
|
|
170
|
+
const isUpward = fromParent === '..' || fromParent.startsWith(`..${sep}`)
|
|
171
|
+
return !isUpward && !isAbsolute(fromParent)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* `realpath` of `target`, or of its nearest existing ancestor joined with the missing segments.
|
|
176
|
+
*/
|
|
177
|
+
async function canonicalPath(
|
|
178
|
+
target: string,
|
|
179
|
+
fs: typeof import('node:fs/promises'),
|
|
180
|
+
{ basename, dirname, join }: typeof import('node:path'),
|
|
181
|
+
): Promise<string> {
|
|
182
|
+
const missing: string[] = []
|
|
183
|
+
let current = target
|
|
184
|
+
while (true) {
|
|
185
|
+
try {
|
|
186
|
+
return join(await fs.realpath(current), ...missing)
|
|
187
|
+
}
|
|
188
|
+
catch (error) {
|
|
189
|
+
const parent = dirname(current)
|
|
190
|
+
if ((error as NodeJS.ErrnoException).code !== 'ENOENT' || parent === current) {
|
|
191
|
+
throw error
|
|
192
|
+
}
|
|
193
|
+
missing.unshift(basename(current))
|
|
194
|
+
current = parent
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Guards `clean` against wiping the file system root, the working directory or one of its parents.
|
|
201
|
+
* Both paths must be canonical.
|
|
202
|
+
*/
|
|
203
|
+
function assertSafeToClean(dir: string, cwd: string, path: typeof import('node:path')): void {
|
|
204
|
+
if (dir === path.parse(dir).root || isInside(dir, cwd, path)) {
|
|
205
|
+
throw new Error(`[vitepress-openapi] Refusing to clean ${dir}: use a dedicated output directory.`)
|
|
206
|
+
}
|
|
207
|
+
}
|