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 "id" to headings #709

Merged
merged 3 commits into from
Oct 9, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 14 additions & 1 deletion dashboard/src/components/ChartView/ChartReadme.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { shallow } from "enzyme";
import { mount, shallow } from "enzyme";
import context from "jest-plugin-context";
import * as React from "react";
import * as ReactMarkdown from "react-markdown";
Expand Down Expand Up @@ -57,3 +57,16 @@ it("renders an error when hasError is set", () => {
);
expect(wrapper.text()).toContain("No README found");
});

it("renders the ReactMarkdown content adding IDs for the titles", () => {
const wrapper = mount(
<ChartReadme
getChartReadme={jest.fn()}
hasError={false}
version="1.2.3"
readme="# _Markdown_ 'Readme_or_not'!"
/>,
);
const component = wrapper.find("#markdown-readme_or_not");
expect(component).toExist();
});
19 changes: 18 additions & 1 deletion dashboard/src/components/ChartView/ChartReadme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ interface IChartReadmeProps {
version: string;
}

// Code from https://github.com/rexxars/react-markdown/issues/69
function flatten(text: string, child: any): any {
Copy link
Contributor

Choose a reason for hiding this comment

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

If we keep this function in this class, we should probably name it better, or instead just have it as an anonymous function in the .reduce

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I moved it to a different component file. I prefer to leave it as it is to maintain fidelity to the linked issue.

return typeof child === "string"
? text + child
: React.Children.toArray(child.props.children).reduce(flatten, text);
}

function HeadingRenderer(props: any) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm I'm not a huge fan of having a Component inline in another component, even though this is small one. @Angelmmiguel what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree with @prydonius. If the component is specific to another one, I usually keep them in the same folder:

CommonComponent
  - CommonComponent.test.tsx
  - CommonComponent.tsx
  - CommonComponent.css
  - SpecificComponent
    - SpecificComponent.test.tsx
    - SpecificComponent.tsx
    - SpecificComponent.css

const children = React.Children.toArray(props.children);
const text = children.reduce(flatten, "");
const slug = text
.toLowerCase()
.replace(/[^a-zA-Z0-9_\s]/g, "") // remove punctuation
.replace(/\s/g, "-"); // replace spaces with dash
return React.createElement("h" + props.level, { id: slug }, props.children);
}

class ChartReadme extends React.Component<IChartReadmeProps> {
public componentWillMount() {
const { getChartReadme, version } = this.props;
Expand All @@ -35,7 +52,7 @@ class ChartReadme extends React.Component<IChartReadmeProps> {
<LoadingWrapper loaded={!!readme}>
{readme && (
<div className="ChartReadme">
<ReactMarkdown source={readme} />
<ReactMarkdown source={readme} renderers={{ heading: HeadingRenderer }} />
</div>
)}
</LoadingWrapper>
Expand Down