Skip to content
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
9 changes: 9 additions & 0 deletions .changeset/rare-gifts-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"react-github-permalink": minor
---

Switch style provider from HLJS to Prism. (Prism supports TSX).

Auto detect language based on file extension.

Adds a `language` prop to override any auto-detection behaviour.
1 change: 1 addition & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
STORYBOOK_GITHUB_TOKEN=${GITHUB_TOKEN_READONLY}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
.pnp.js
.yarn/install-state.gz

.env

# testing
/coverage

Expand Down
15 changes: 0 additions & 15 deletions .storybook/preview.ts

This file was deleted.

22 changes: 22 additions & 0 deletions .storybook/preview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Preview } from "@storybook/react";
import './global.css';
import { GithubPermalinkProvider } from "../src/library/config/GithubPermalinkContext";

const preview: Preview = {
parameters: {
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/i,
},
},
},
decorators: [(Story) => (
<div style={{ margin: '3em' }} >
<GithubPermalinkProvider githubToken={process.env.STORYBOOK_GITHUB_TOKEN}>
<Story />
</GithubPermalinkProvider>
</div>)]
};

export default preview;
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ I highly rate the [`vscode-copy-github-permalink` plugin](https://marketplace.vi
https://codesandbox.io/s/exciting-nova-js5zlk?file=/src/App.js


## Language Support

Langauge is naively auto detected based on file extension. See [logic here for all auto-detected languages](https://github.com/dwjohnston/react-github-permalink/pull/73/files#diff-b6feb43e40d6eae1cba733450d691be8f83a1a50ecbff1b890cd343b2039ece1).

If this does not suit you, you can override the autodetected langauage with the `language` prop.




## RSC Compatibility / Three modes of operation

This package is compatible with Next 13+ and the components can be used as RSCs if you wish.
Expand Down
20 changes: 20 additions & 0 deletions sample_files/sample.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use an official Node.js runtime as a parent image
FROM node:18-alpine

# Set the working directory
WORKDIR /app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code
COPY . .

# Expose the app port
EXPOSE 3000

# Start the application
CMD ["npm", "start"]
19 changes: 19 additions & 0 deletions sample_files/sample.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Sample HTML Page</title>
</head>

<body>
<h1>Welcome to the Sample HTML Page</h1>
<p>This is a simple HTML file for demonstration purposes.</p>
<ul>
<li>First item</li>
<li>Second item</li>
<li>Third item</li>
</ul>
</body>

</html>
15 changes: 15 additions & 0 deletions sample_files/sample.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<library>
<book id="1">
<title>Introduction to XML</title>
<author>Jane Doe</author>
<year>2022</year>
<genre>Technology</genre>
</book>
<book id="2">
<title>Learning React</title>
<author>John Smith</author>
<year>2023</year>
<genre>Programming</genre>
</book>
</library>
11 changes: 11 additions & 0 deletions sample_files/sample1.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function Foo(props: { name: string }) {
return <div>{props.name}</div>

}

export function Demo() {
return <div>
<Foo name="bar" />
Hello world!
</div>
}
27 changes: 27 additions & 0 deletions src/library/GithubPermalink/GithubPermalink.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,33 @@ export const Primary: Story = {
),
};

export const DifferentLanguages: Story = {
render: () => (<div>
<p>Go</p>
<GithubPermalink permalink="https://github.com/dwjohnston/react-github-permalink/blob/5b15aa07e60af4e317086f391b28cadf9aae8e1b/sample_files/sample1.go#L1-L5" />

<p>JavaScript</p>
<GithubPermalink permalink="https://github.com/dwjohnston/react-github-permalink/blob/bc75e8fe2d1c0395c9443afe1837f453c05a7698/sample_files/sample1.js#L3-L17" />

<p>SQL</p>
<GithubPermalink permalink="https://github.com/dwjohnston/react-github-permalink/blob/bc75e8fe2d1c0395c9443afe1837f453c05a7698/sample_files/sample1.sql#L11-L21" />

<p>TSX</p>
<GithubPermalink permalink="https://github.com/dwjohnston/react-github-permalink/blob/242681a9df549adcc9a7fca0d8421d98b7e312c4/sample_files/sample1.tsx#L1-L11" />

<p>Docker file</p>
<GithubPermalink permalink="https://github.com/dwjohnston/react-github-permalink/blob/bd98ef231beeb18ed77207216464fc8e454dd721/sample_files/sample.dockerfile#L1-L20" />

<p>XML</p>
<GithubPermalink permalink="https://github.com/dwjohnston/react-github-permalink/blob/bd98ef231beeb18ed77207216464fc8e454dd721/sample_files/sample.xml#L1-L15" />

<p>HTML</p>
<GithubPermalink permalink="https://github.com/dwjohnston/react-github-permalink/blob/bd98ef231beeb18ed77207216464fc8e454dd721/sample_files/sample.html#L1-L19" />

</div>
),
};

export const WithBackground: Story = {
render: () => (
<div style={{ backgroundColor: "pink", padding: "1em" }}>
Expand Down
7 changes: 3 additions & 4 deletions src/library/GithubPermalink/GithubPermalink.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
"use client"

import {useContext, useEffect, useState } from "react";
import { useContext, useEffect, useState } from "react";
import { GithubPermalinkDataResponse, GithubPermalinkContext } from "../config/GithubPermalinkContext";
import { useMediaQuery } from "react-responsive";
import { GithubPermalinkBase, GithubPermalinkBaseProps } from "./GithubPermalinkBase";

type GithubPermalinkProps = Omit<GithubPermalinkBaseProps, "data"> & {permalink: string};
type GithubPermalinkProps = Omit<GithubPermalinkBaseProps, "data"> & { permalink: string };
export function GithubPermalink(props: GithubPermalinkProps) {

const { permalink } = props;
Expand All @@ -28,7 +27,7 @@ export function GithubPermalink(props: GithubPermalinkProps) {
}


return <GithubPermalinkBase data={data} {...props}/>
return <GithubPermalinkBase data={data} {...props} />
}


11 changes: 7 additions & 4 deletions src/library/GithubPermalink/GithubPermalinkBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,31 @@ import { GithubSvg } from "../GithubSvg/GithubSvg";
import { PropsWithChildren } from "react";
import { SyntaxHighlight } from "../SyntaxHighlight/SyntaxHighlight";
import { formatForLineExclusions } from "./formatLineExclusions";
import { CopySvg } from "../images/CopySvg";
import { CopyButton } from "../common/CopyButton/CopyButton";
import { getLanguageFromPath } from "../utils/getLanguageFromPath";
import { AvailableLanguagesPrism } from "../SyntaxHighlight/availableLanguagesPrism";

export type GithubPermalinkBaseProps = {
className?: string;
permalink: string;
excludeLines?: Array<[from: number, to: number]>;
excludeText?: string;
data: GithubPermalinkDataResponse;
language?: AvailableLanguagesPrism;
}



export function GithubPermalinkBase(props: GithubPermalinkBaseProps) {

const { data, permalink, excludeLines, excludeText = "<snip>" } = props;
const { data, permalink, excludeLines, excludeText = "<snip>", } = props;



if (data.status === "ok") {

const formatedLineExclusions = formatForLineExclusions(data, excludeLines);
const language = props.language ?? getLanguageFromPath(data.path);

const clipboard = formatedLineExclusions.reduce((acc, cur) => {
if (cur.isExclude) {
Expand All @@ -41,11 +44,11 @@ export function GithubPermalinkBase(props: GithubPermalinkBaseProps) {

{formatedLineExclusions.map((v) => {
if (v.isExclude) {
return <SyntaxHighlight className="hide-line-numbers" text={excludeText} startingLineNumber={v.from} key={v.from}/>
return <SyntaxHighlight className="hide-line-numbers" text={excludeText} startingLineNumber={v.from} language={language} key={v.from} />

}

return <SyntaxHighlight text={v.lines.join("\n")} startingLineNumber={v.from} key={v.from}/>
return <SyntaxHighlight text={v.lines.join("\n")} startingLineNumber={v.from} language={language} key={v.from} />

})}

Expand Down
Loading