Skip to content

Commit

Permalink
Escape hash in code/codeblocks (#504)
Browse files Browse the repository at this point in the history
* Escape hash in code/codeblocks

* format

* pin test accelerarte
  • Loading branch information
mishig25 authored May 30, 2024
1 parent 5dab464 commit 5b49e67
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 11 deletions.
1 change: 1 addition & 0 deletions .github/workflows/accelerate_doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
with:
repository: 'huggingface/accelerate'
path: accelerate
ref: 16eb6d76bf987c7d8d877ce5152f2e29878eab37

- name: Loading cache.
uses: actions/cache@v2
Expand Down
17 changes: 17 additions & 0 deletions kit/preprocessors/hashInCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { replaceAsync } from "./utils.js";

// Preprocessor that converts `#` char inside code/codeblocks into its html5 entity `#`
// otherwise, an extra new space was being rendered
export const hashInCodePreprocess = {
markup: async ({ content }) => {
const REGEX_CODE_BLOCK = /```.*?```/gs;
const REGEX_INLINE_CODE = /`.*?`/gs;
content = await replaceAsync(content, REGEX_CODE_BLOCK, async (codeContent) => {
return codeContent.replaceAll("#", "#");
});
content = await replaceAsync(content, REGEX_INLINE_CODE, async (codeContent) => {
return codeContent.replaceAll("#", "#");
});
return { code: content };
},
};
1 change: 1 addition & 0 deletions kit/preprocessors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export { inferenceSnippetPreprocess } from "./inferenceSnippet.js";
export { tokenizersLangPreprocess } from "./tokenizersLang.js";
export { mdsvexPreprocess } from "./mdsvex/index.js";
export { hfOptionsPreprocess } from "./hfOptions.js";
export { hashInCodePreprocess } from "./hashInCode.js";
10 changes: 1 addition & 9 deletions kit/preprocessors/mdsvex/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import htmlTags from "html-tags";
import { readdir } from "fs/promises";
import path from "path";
import cheerio from "cheerio";
import { renderSvelteChars } from "../utils.js";

/**
* inside `<code>` html elements, we need to replace `&amp;` with `&`
Expand Down Expand Up @@ -43,15 +44,6 @@ export const mdsvexPreprocess = {
},
};

/**
* Render escaped characters like `<`, `{`.
* used for Doc
* @param {string} code
*/
function renderSvelteChars(code) {
return code.replace(/&amp;lcub;/g, "{").replace(/&amp;lt;/g, "<");
}

/**
* Latex support in mdsvex
* @param {string} content
Expand Down
7 changes: 5 additions & 2 deletions kit/preprocessors/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ export async function replaceAsync(string, searchValue, replacer) {
}

/**
* Render escaped characters like `<`, `{`.
* Render escaped characters like `<`, `{`, '#'.
* used for Doc
* @param {string} code
*/
export function renderSvelteChars(code) {
return code.replace(/&amp;lcub;/g, "{").replace(/&amp;lt;/g, "<");
return code
.replace(/&amp;lcub;/g, "{")
.replace(/&amp;lt;/g, "<")
.replace(/&amp;num;/g, "#");
}

/**
Expand Down
2 changes: 2 additions & 0 deletions kit/svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import {
inferenceSnippetPreprocess,
tokenizersLangPreprocess,
hfOptionsPreprocess,
hashInCodePreprocess,
} from "./preprocessors/index.js";

/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://kit.svelte.dev/docs/integrations#preprocessors
// for more information about preprocessors
preprocess: [
hashInCodePreprocess,
docstringPreprocess,
frameworkcontentPreprocess,
inferenceSnippetPreprocess,
Expand Down

0 comments on commit 5b49e67

Please sign in to comment.