Skip to content

Commit 5b49e67

Browse files
authored
Escape hash in code/codeblocks (#504)
* Escape hash in code/codeblocks * format * pin test accelerarte
1 parent 5dab464 commit 5b49e67

File tree

6 files changed

+27
-11
lines changed

6 files changed

+27
-11
lines changed

.github/workflows/accelerate_doc.yml

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ jobs:
2323
with:
2424
repository: 'huggingface/accelerate'
2525
path: accelerate
26+
ref: 16eb6d76bf987c7d8d877ce5152f2e29878eab37
2627

2728
- name: Loading cache.
2829
uses: actions/cache@v2

kit/preprocessors/hashInCode.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { replaceAsync } from "./utils.js";
2+
3+
// Preprocessor that converts `#` char inside code/codeblocks into its html5 entity `#`
4+
// otherwise, an extra new space was being rendered
5+
export const hashInCodePreprocess = {
6+
markup: async ({ content }) => {
7+
const REGEX_CODE_BLOCK = /```.*?```/gs;
8+
const REGEX_INLINE_CODE = /`.*?`/gs;
9+
content = await replaceAsync(content, REGEX_CODE_BLOCK, async (codeContent) => {
10+
return codeContent.replaceAll("#", "#");
11+
});
12+
content = await replaceAsync(content, REGEX_INLINE_CODE, async (codeContent) => {
13+
return codeContent.replaceAll("#", "#");
14+
});
15+
return { code: content };
16+
},
17+
};

kit/preprocessors/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export { inferenceSnippetPreprocess } from "./inferenceSnippet.js";
44
export { tokenizersLangPreprocess } from "./tokenizersLang.js";
55
export { mdsvexPreprocess } from "./mdsvex/index.js";
66
export { hfOptionsPreprocess } from "./hfOptions.js";
7+
export { hashInCodePreprocess } from "./hashInCode.js";

kit/preprocessors/mdsvex/index.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import htmlTags from "html-tags";
66
import { readdir } from "fs/promises";
77
import path from "path";
88
import cheerio from "cheerio";
9+
import { renderSvelteChars } from "../utils.js";
910

1011
/**
1112
* inside `<code>` html elements, we need to replace `&amp;` with `&`
@@ -43,15 +44,6 @@ export const mdsvexPreprocess = {
4344
},
4445
};
4546

46-
/**
47-
* Render escaped characters like `<`, `{`.
48-
* used for Doc
49-
* @param {string} code
50-
*/
51-
function renderSvelteChars(code) {
52-
return code.replace(/&amp;lcub;/g, "{").replace(/&amp;lt;/g, "<");
53-
}
54-
5547
/**
5648
* Latex support in mdsvex
5749
* @param {string} content

kit/preprocessors/utils.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@ export async function replaceAsync(string, searchValue, replacer) {
2424
}
2525

2626
/**
27-
* Render escaped characters like `<`, `{`.
27+
* Render escaped characters like `<`, `{`, '#'.
2828
* used for Doc
2929
* @param {string} code
3030
*/
3131
export function renderSvelteChars(code) {
32-
return code.replace(/&amp;lcub;/g, "{").replace(/&amp;lt;/g, "<");
32+
return code
33+
.replace(/&amp;lcub;/g, "{")
34+
.replace(/&amp;lt;/g, "<")
35+
.replace(/&amp;num;/g, "#");
3336
}
3437

3538
/**

kit/svelte.config.js

+2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import {
88
inferenceSnippetPreprocess,
99
tokenizersLangPreprocess,
1010
hfOptionsPreprocess,
11+
hashInCodePreprocess,
1112
} from "./preprocessors/index.js";
1213

1314
/** @type {import('@sveltejs/kit').Config} */
1415
const config = {
1516
// Consult https://kit.svelte.dev/docs/integrations#preprocessors
1617
// for more information about preprocessors
1718
preprocess: [
19+
hashInCodePreprocess,
1820
docstringPreprocess,
1921
frameworkcontentPreprocess,
2022
inferenceSnippetPreprocess,

0 commit comments

Comments
 (0)