Skip to content

Commit

Permalink
Merge pull request #33 from hyesungoh/#31
Browse files Browse the repository at this point in the history
TOC showing text value
  • Loading branch information
hyesungoh authored Apr 13, 2022
2 parents 7918170 + 76b84d4 commit 323343a
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 15 deletions.
File renamed without changes.
1 change: 0 additions & 1 deletion apps/blog/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />
/// <reference types="jest" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import DateAndCategoryLink from './DateAndCategoryLink';
const DATE = '2022-04-14';
const CATEGORY = 'Guide';

describe('blog - component - DateAbdCategoryLink', () => {
describe('blog - component - DateAndCategoryLink', () => {
it('should', () => {
render(<DateAndCategoryLink date={DATE} category={CATEGORY} />);
expect(screen.getByText(CATEGORY)).toBeInTheDocument();
Expand Down
21 changes: 21 additions & 0 deletions apps/blog/src/components/TOC/TOC.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react';
import { render, screen } from '@testing-library/react';

import TOC from './TOC';

jest.spyOn(React, 'useRef').mockImplementation(() => ({
current: {
observe: jest.fn(),
disconnect: jest.fn(),
},
}));
jest.spyOn(React, 'useEffect').mockImplementation(f => f());

describe('blog - TOC', () => {
beforeAll(() => {});

it('does not render when zero headings', () => {
render(<TOC />);
expect(screen.queryByText('Contents')).not.toBeInTheDocument();
});
});
19 changes: 12 additions & 7 deletions apps/blog/src/components/TOC/TOC.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useEffect, useRef, useState } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import { useRouter } from 'next/router';
import styled from '@emotion/styled';
import { Link, NextUITheme, useTheme } from '@nextui-org/react';
import { useMediaQuery } from 'core';

import getHeadings from '../../utils/getHeadings';
import getHeadings, { IHeading } from '../../utils/getHeadings';

function TOC() {
const [headings, setHeadings] = useState<string[]>([]);
const [headings, setHeadings] = useState<IHeading[]>([]);
const router = useRouter();
const { theme } = useTheme();

Expand All @@ -16,7 +16,7 @@ function TOC() {
}, [router]);

const activeId = useScrollSpy({
ids: headings,
ids: headings.map(heading => heading.id),
options: {
rootMargin: '0% 0% -80% 0%',
},
Expand All @@ -33,8 +33,13 @@ function TOC() {
<Ul>
{headings.map((heading, index) => (
<Li key={index}>
<Anchor href={`#${heading}`} underline className={heading === activeId ? 'active' : ''} theme={theme}>
{heading.replaceAll('-', ' ')}
<Anchor
href={`#${heading.id}`}
underline
className={heading.id === activeId ? 'active' : ''}
theme={theme}
>
{heading.text}
</Anchor>
</Li>
))}
Expand All @@ -56,7 +61,7 @@ function useScrollSpy({ ids, options }: HookProps) {
const observer = useRef<IntersectionObserver>();

useEffect(() => {
const elements = ids.map(id => document.getElementById(id));
const elements = ids.map(id => document.getElementById(`${id}`));

if (observer.current) {
observer.current.disconnect();
Expand Down
16 changes: 13 additions & 3 deletions apps/blog/src/utils/getHeadings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
function getHeadings(): string[] {
export interface IHeading {
id: string;
text: string;
}

function getHeadings() {
if (typeof document === 'undefined') return [];
const headings = document.getElementsByClassName('heading') as HTMLCollection;
return Array.from(headings).map((e: HTMLHtmlElement) => e.id);

const headings = document.querySelectorAll<HTMLHeadingElement>('.heading');

return Array.from(headings).map(element => ({
id: element.id,
text: element.innerText,
}));
}

export default getHeadings;
3 changes: 2 additions & 1 deletion apps/blog/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"extends": "tsconfig/config-nextjs.json",
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
"exclude": ["node_modules", "**/*.spec.ts", "**/*.test.ts", "**/*.spec.tsx", "**/*.test.tsx"]
// note: extending exclude must have baseUrl
}
1 change: 0 additions & 1 deletion apps/resume/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />
/// <reference types="jest" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
3 changes: 2 additions & 1 deletion apps/resume/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"extends": "tsconfig/config-nextjs.json",
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
"exclude": ["node_modules", "**/*.spec.ts", "**/*.test.ts", "**/*.spec.tsx", "**/*.test.tsx"]
// note: extending exclude must have baseUrl
}

2 comments on commit 323343a

@vercel
Copy link

@vercel vercel bot commented on 323343a Apr 13, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

comet-land-blog – ./apps/blog

comet-land-blog.vercel.app
comet-land-blog-git-main-hyesungoh.vercel.app
comet-land-blog-hyesungoh.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 323343a Apr 13, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

comet-land-resume – ./apps/resume

comet-land-resume-git-main-hyesungoh.vercel.app
comet-land-resume.vercel.app
comet-land-resume-hyesungoh.vercel.app

Please sign in to comment.