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

Escape hash in code/codeblocks #504

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading