Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] Update plugin #20701

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Saving
  • Loading branch information
ronny-mysten committed Dec 18, 2024
commit 4fd9ccc479cf87c8c703353847f458457cdb56a0
2 changes: 2 additions & 0 deletions docs/content/guides/developer/app-examples/trustless-swap.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ Click the titles at the top of codeblocks to open the relevant source file in Gi
{@inject: examples/trading/contracts/escrow/sources/lock.move noComments}
</details>

{@inject: github:MystenLabs/suins-contracts/packages/day_one/sources/bogo.move}

After a trade is initiated, you don't want the trading party to modify the object they agreed to trade. Imagine you're trading in-game items and you agree to trade a weapon with all its attachments, and its owner strips all its attachments just before the trade.

In a traditional trade, a third party typically holds the items in escrow to make sure they are not tampered with before the trade completes. This requires either trusting that the third party won't tamper with it themselves, paying the third party to ensure that doesn't happen, or both.
Expand Down
1 change: 0 additions & 1 deletion docs/site/src/plugins/inject-code/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const injectCode = (context, opts) => {
const loaderOptions = {
replacements: opts.replacements,
embeds: opts.embeds,
sharedFolders: opts.sharedFolders,
};

return {
Expand Down
76 changes: 64 additions & 12 deletions docs/site/src/plugins/inject-code/injectLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,83 @@

const fs = require("fs");
const path = require("path");
const https = require("https");
const utils = require("./utils.js");

const addCodeInject = function (source) {
const GITHUB = "https://raw.githubusercontent.com/";
const GITHUB_RAW = "refs/heads/main/";

const addCodeInject = async function (source) {
let fileString = source;
const callback = this.async();
const options = this.getOptions();

const markdownFilename = path.basename(this.resourcePath);
const repoPath = path.join(__dirname, "../../../../..");

// Do not load and render markdown files without docusaurus header.
// These files are only used to be included in other files and should not generate their own web page
if (fileString.length >= 3 && fileString.substring(0, 3) !== "---") {
return callback && callback(null, "");
}

const fetchFile = (url) => {
return new Promise((res, rej) => {
let data = "";
https
.get(url, (response) => {
if (response.statusCode !== 200) {
console.error(
`Failed to fetch GitHub data: ${response.statusCode}`,
);
response.resume();
return;
}

response.on("data", (chunk) => {
data += chunk;
});

response.on("end", () => {
res(data);
});
})
.on("error", (err) => {
rej(`Error: ${err.message}`);
});
});
};

function addMarkdownIncludes(fileContent) {
let res = fileContent;
const matches = fileContent.match(/(?<!`)\{@\w+: .+\}/g);
if (matches) {
matches.forEach((match) => {
matches.forEach(async (match) => {
const replacer = new RegExp(match, "g");
const key = "{@inject: ";

if (match.startsWith(key)) {
const parts = match.split(" ");
const [, , ...options] = parts.length > 2 ? parts : [];
let injectFileFull = parts[1].replace(/\}$/, "");

const injectFile = injectFileFull.split("#")[0];

const isSuiRepo = !injectFile.match(/^github:/i);
const githubOrg = isSuiRepo
? ""
: injectFile.split("/")[0].split(":")[1];
const githubRepo = isSuiRepo ? "" : injectFile.split("/")[1];
const repoPath = isSuiRepo
? path.join(__dirname, "../../../../..")
: path.join(
GITHUB,
githubOrg,
githubRepo,
GITHUB_RAW,
injectFile.split(githubRepo)[1],
);
let fileExt = injectFile.substring(injectFile.lastIndexOf(".") + 1);
let language = "";
const fullPath = path.join(repoPath, injectFile);
const fullPath = isSuiRepo
? path.join(repoPath, injectFile)
: repoPath;

switch (fileExt) {
case "lock":
Expand Down Expand Up @@ -64,10 +107,19 @@ const addCodeInject = function (source) {
const isMove = language === "move";
const isTs = language === "ts" || language === "js";

if (fs.existsSync(fullPath)) {
let injectFileContent = fs
.readFileSync(fullPath, "utf8")
.replaceAll(`\t`, " ");
if (fs.existsSync(fullPath) || !isSuiRepo) {
let injectFileContent = "";
if (isSuiRepo) {
injectFileContent = fs
.readFileSync(fullPath, "utf8")
.replaceAll(`\t`, " ");
} else {
try {
injectFileContent = await fetchFile(fullPath);
} catch {
injectFileContent = "Problem loading GitHub file.";
}
}
const marker =
injectFileFull.indexOf("#") > 0
? injectFileFull.substring(injectFileFull.indexOf("#"))
Expand Down Expand Up @@ -570,7 +622,7 @@ const addCodeInject = function (source) {
return res;
}

fileString = replacePlaceHolders(addMarkdownIncludes(fileString));
fileString = replacePlaceHolders(await addMarkdownIncludes(fileString));

return callback && callback(null, fileString);
};
Expand Down