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

Add Code Highlighting to RemoteContent #3251

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
103 changes: 103 additions & 0 deletions astro/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"astro": "^4.7.0",
"astro-compress": "^2.0.8",
"astro-index-pages": "src/integrations/astro-index-pages",
"astro-remote": "^0.3.3",
"marked": "^7.0.1",
"mermaid": "^9.1.6",
"pagefind": "^1.1.0",
Expand Down
7 changes: 7 additions & 0 deletions astro/src/components/CodeBlock.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
import { Code } from "astro/components";

const { lang, code, ...props } = Astro.props;
---

<Code code={code} lang={lang} {...props} />
50 changes: 33 additions & 17 deletions astro/src/components/RemoteContent.astro
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
---
/**
* Remote content fetcher. Remote content is assumed to be markdown.
*/
import { marked } from 'marked';
* Remote content fetcher. Remote content is assumed to be markdown.
*/
import { Markdown } from "astro-remote";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not found, did you include this in the package.json?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my bad @lyleschemmerling I think I installed before I created the new branch.

import CodeBlock from "./CodeBlock.astro";

export interface Props {
/**
Expand All @@ -15,9 +16,9 @@ export interface Props {
/**
* Optional tags marking beginning and end of content to include
* Loosely based on https://docs.asciidoctor.org/asciidoc/latest/directives/include-tagged-regions/
* Note: Currently only supports one set of tags. If the tags are enclosed in comments, make sure to have the comments on one line.
* Note: Currently only supports one set of tags. If the tags are enclosed in comments, make sure to have the comments on one line.
*
* See https://raw.githubusercontent.com/FusionAuth/fusionauth-example-scripts/main/client-side-password-rules/README.md and
* See https://raw.githubusercontent.com/FusionAuth/fusionauth-example-scripts/main/client-side-password-rules/README.md and
* src/content/docs/customize/look-and-feel/client-side-password-rule-validation.mdx for an example.
*/
tags?: string;
Expand All @@ -35,44 +36,59 @@ try {
} else {
const matchLines = url.match(/.*#L(\d+)(-L(\d+))?$/);
if (matchLines) {
remoteSelectedContent = selectLines(remoteContent, matchLines[1], matchLines[3]);
remoteSelectedContent = selectLines(
remoteContent,
parseInt(matchLines[1]),
parseInt(matchLines[3]),
);
} else {
remoteSelectedContent = remoteContent.trim();
}
}

remoteSelectedContent = marked.parse(remoteSelectedContent);

} catch (e) {
console.log("Couldn't retrieve remote code",e);
console.log("Couldn't retrieve remote code", e);
}

function selectTagged(content: string, tags: string): string {
const lines = content.split("\n");

const startLine = lines.findIndex((line) => line.includes(`tag::${tags}`));
const endLine = lines.findIndex((line) => line.includes(`end::${tags}`))
const endLine = lines.findIndex((line) => line.includes(`end::${tags}`));

// remove trailing multi-line HTML comment
if (startLine < lines.length - 1) {
lines[startLine + 1] = lines[startLine + 1].replace('-->', '');
lines[startLine + 1] = lines[startLine + 1].replace("-->", "");
}

// remove preceding multi-line HTML comment
lines[endLine-1] = lines[endLine-1].replace('<!--', '');
lines[endLine - 1] = lines[endLine - 1].replace("<!--", "");

return lines.slice(startLine + 1, endLine).join("\n").replace(/\s+$/g, '');
return lines
.slice(startLine + 1, endLine)
.join("\n")
.replace(/\s+$/g, "");
}

function selectLines(content: string, lineNum: string, lineEnd: string): string {
function selectLines(
content: string,
lineNum: number,
lineEnd: number,
): string {
const lines = content.split("\n");
if (lineNum && lineEnd) {
return lines.slice(lineNum-1, lineEnd-1).join("\n");
return lines.slice(lineNum - 1, lineEnd - 1).join("\n");
} else if (lineNum) {
return lines[lineNum - 1];
} else {
return content;
}
}
---
<article set:html={remoteSelectedContent} />

<article>
<Markdown
content={remoteSelectedContent}
sanitize={{ allowComponents: true }}
components={{ CodeBlock }}
/>
</article>
Loading