-
Notifications
You must be signed in to change notification settings - Fork 69
Raphael/docusaurus migration #701
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
Conversation
links images etc ...
zero broken links!
versionned doc
|
|
||
| // Escape backticks and dollar signs for template literals | ||
| const escapeForTemplate = (str: string) => { | ||
| return str.replace(/`/g, '\\`').replace(/\$/g, '\\$'); |
Check failure
Code scanning / CodeQL
Incomplete string escaping or encoding High
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 5 days ago
The code block at line 43 defines escapeForTemplate, which attempts to escape backticks and dollar signs from the code content that will be injected into a template literal of the form `${...}`. However, it does not escape backslashes, which could result in a malformed string. The best way to fix this is to first escape all backslashes, then escape backticks and dollar signs. The fix is to update the escapeForTemplate function to use:
str.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\$/g, '\\$')This order ensures that pre-existing escape sequences in str are handled correctly and no malformed escapes are introduced.
Edit only escapeForTemplate in docusaurus-docs/src/components/RunnableCodeBlock/index.tsx, and make no changes elsewhere. No external dependencies are needed.
-
Copy modified line R43
| @@ -40,7 +40,7 @@ | ||
|
|
||
| // Escape backticks and dollar signs for template literals | ||
| const escapeForTemplate = (str: string) => { | ||
| return str.replace(/`/g, '\\`').replace(/\$/g, '\\$'); | ||
| return str.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\$/g, '\\$'); | ||
| }; | ||
|
|
||
| const escapedCode = escapeForTemplate(codeContent); |
|
|
||
| // Escape backticks and dollar signs for template literals | ||
| const escapeForTemplate = (str: string) => { | ||
| return str.replace(/`/g, '\\`').replace(/\$/g, '\\$'); |
Check failure
Code scanning / CodeQL
Incomplete string escaping or encoding High
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 5 days ago
To correctly escape input for inclusion in a Go string literal using backticks (which start/end with backticks and in which only the backtick itself cannot appear unescaped), one does not generally need to escape backslashes—because Go's raw string literals (backtick) will treat backslashes as normal characters. However, in template literals in JavaScript, backslashes can cause escaping bugs if not properly handled. Since the intention is to embed arbitrary code into a Go raw string literal (inside backticks), we're only required to escape the backtick itself. The original code also escaped $ for use inside a JS template literal (so that ${} would not be evaluated). But, the current logic might still fail if there are backslashes followed by backticks or dollar signs. The safest approach, for JS template literal embedding, is to escape all three: backtick, dollar sign, and backslash in that order—and use a single replace with a function, or at least escape backslash first to avoid double-escaping.
The best fix is:
- Escape backslashes (
\) first, then backticks (`), then dollar signs ($). - Use a single replace, or chain in the correct order.
- Use a well-tested library like lodash.escape if you want robust HTML/joined escaping, but for this specific context, a simple regex suffices.
Required changes:
- In
escapeForTemplate, change the implementation to escape backslash first, then backtick, then dollar sign. - No additional dependencies are needed.
-
Copy modified lines R43-R47
| @@ -40,7 +40,11 @@ | ||
|
|
||
| // Escape backticks and dollar signs for template literals | ||
| const escapeForTemplate = (str: string) => { | ||
| return str.replace(/`/g, '\\`').replace(/\$/g, '\\$'); | ||
| // Escape backslash, then backtick, then dollar sign (in that order) | ||
| return str | ||
| .replace(/\\/g, '\\\\') | ||
| .replace(/`/g, '\\`') | ||
| .replace(/\$/g, '\\$'); | ||
| }; | ||
|
|
||
| const escapedCode = escapeForTemplate(codeContent); |
| DgraphGrpc.DgraphStub stub = DgraphGrpc.newStub(channel); | ||
| DgraphClient dgraphClient = new DgraphClient(stub); | ||
|
|
||
| String query = "${escapedCode.replace(/"/g, '\\"')}"; |
Check failure
Code scanning / CodeQL
Incomplete string escaping or encoding High
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 5 days ago
To fix the problem, we must ensure that all special characters in the query string are properly escaped for safe injection in a Java string literal. In Java, both double quotes (") and backslashes (\) need to be escaped (i.e., \" and \\). The best fix is to create a helper function (analogous to escapeForTemplate) that escapes both backslashes and double quotes for Java strings. We should apply this function specifically for the Java code block (line 114), replacing the existing direct .replace(/"/g, '\\"') logic, and ensure comprehensive escaping even if both characters are present.
Modify the file docusaurus-docs/src/components/RunnableCodeBlock/index.tsx as follows:
- Above the return statement, introduce a helper function, e.g.,
escapeForJavaString, which:- Replaces all
\with\\. - Then replaces all
"with\".
- Replaces all
- Pass
escapedJavaCode(output of this function applied tocodeContent) into the Java string literal on line 114.
No external dependency is required; the builtin JavaScript .replace() with the g flag suffices.
-
Copy modified lines R48-R53 -
Copy modified line R120
| @@ -45,6 +45,12 @@ | ||
|
|
||
| const escapedCode = escapeForTemplate(codeContent); | ||
|
|
||
| // Helper function to escape input for Java string literals | ||
| const escapeForJavaString = (str: string) => { | ||
| return str.replace(/\\/g, '\\\\').replace(/"/g, '\\"'); | ||
| }; | ||
| const escapedJavaCode = escapeForJavaString(codeContent); | ||
|
|
||
| return ( | ||
| <div className={styles.runnable}> | ||
| <Tabs> | ||
| @@ -111,7 +117,7 @@ | ||
| DgraphGrpc.DgraphStub stub = DgraphGrpc.newStub(channel); | ||
| DgraphClient dgraphClient = new DgraphClient(stub); | ||
|
|
||
| String query = "${escapedCode.replace(/"/g, '\\"')}"; | ||
| String query = "${escapedJavaCode}"; | ||
|
|
||
| Transaction txn = dgraphClient.newTransaction(); | ||
| try { |
migrate site to docusaurus-docs