Hi!
I’m migrating a website written in Jekyll to Hugo, that uses the following Jekyll plugin: https://github.com/bwillis/jekyll-github-sample/tree/master/lib/jekyll_github_sample
What I need to do is extract text from given github file and put it in content markdown/highlight function.
So far I’ve created a shortcode with a JS script that fetches a file from GitHub and extracts text between delimiters. However, I don’t know how to pass the result of that script to Hugo markdown and use the highlighter.
Here’s my shortcode (github_sample.html
):
{{ $rawGithubUrl := "https://raw.githubusercontent.com" }}
{{ $path := .Get 0 }}
{{ $tag := .Get 1 }}
<script type="text/javascript">
const removeBlobFromUrl = url => url.split("/").filter(part => part !== "blob").join("/");
const createGithubRawUrl = path => {{ $rawGithubUrl }} + removeBlobFromUrl(path);
const getContentBetweenDelimiters = text => {
const regexString = "\\[START " + {{ $tag }} + "]\n[\\s\\S]*?\n.*\\[END " + {{ $tag }} + "]";
const re = new RegExp(regexString);
const match = text.match(re)[0].split("\n");
match.shift();
match.pop();
return match.join("\n");
}
fetch(createGithubRawUrl({{ $path }}))
.then(response => response.text())
.then(fileContent => {
const result = getContentBetweenDelimiters(fileContent)
// TODO: PASS THE RESULT TO CONTENT
console.log(result)
})
</script>
And here’s how I need to use it:
{{< highlight py >}}
{{< github_sample "/path/to/github/file.py" delimiter_tag >}}
{{< /highlight >}}
I’d appreciate any help!