Skip to content

Commit

Permalink
fix: fix toc and refactor slugify
Browse files Browse the repository at this point in the history
  • Loading branch information
k8isdead authored and borkmann committed Sep 6, 2022
1 parent 9b479b4 commit cfced91
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 34 deletions.
5 changes: 2 additions & 3 deletions gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const path = require("path");
const createPaginatedPages = require("gatsby-paginate");
const _ = require("lodash");
const { createTags } = require("./src/utils/createTags");

const createMetaPage = ({
type,
Expand Down Expand Up @@ -163,14 +162,14 @@ exports.createPages = ({ actions, graphql }) => {
const categories = _(edges)
.map((item) => _.get(item, ["node", "frontmatter", "categories"], []))
.flatten()
.map((item) => createTags(item))
.map((item) => slugify(item, {lower:true}))
.uniq()
.value();

categories.forEach((category) => {
const filteredEdges = edges.filter(({ node }) =>
node.frontmatter.categories
.map((category) => createTags(category))
.map((category) => slugify(category, {lower:true}))
.includes(category)
);

Expand Down
1 change: 1 addition & 0 deletions 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"react-modal-video": "^1.2.9",
"react-popover": "^0.5.10",
"react-refresh": "^0.14.0",
"slugify": "^1.6.5",
"string-similarity": "^4.0.4"
},
"keywords": [
Expand Down
32 changes: 17 additions & 15 deletions src/common/blog-post/Post.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useState, useEffect, Fragment } from "react";
import React, { useState, useEffect } from "react";
import { format, parseISO } from "date-fns";
import slugify from "slugify";

import parseHtml from "../../../scripts/parse-html";

Expand All @@ -12,10 +13,7 @@ const TableOfContents = () => {
const getTocId = (element) => {
return element.id
? element.id
: element.textContent
.toLowerCase()
.replace(/ /g, "-")
.replace(/[^a-z0-9\-]/g, "");
: slugify(element.textContent, {replacement: '-', remove: /[^a-z0-9\-]/g, lower: true})
};

useEffect(() => {
Expand All @@ -27,26 +25,22 @@ const TableOfContents = () => {
});
setToc(toc);
});
console.log(toc);


return (
<Fragment>
<details className='blog-toc' open>
<summary>Table of Contents</summary>
<ul>
{toc.map((item, index) => {
const level = +item.tagName[1] - 2;
const style = { paddingLeft: `${20 * level}px` };
let className = "toc-item";
return (
<li className={className} style={style} key={index}>
<li className="toc-item" style={style} key={index}>
<a href={`#${item.id}`}>{item.textContent}</a>
</li>
);
})}
</ul>
</details>
</Fragment>
);
};

Expand All @@ -55,7 +49,17 @@ const Post = ({ post, full }) => {
const { hasPreview, previewHtml, mainHtml, previewDescription } =
parseHtml(post);

const html = full ? mainHtml : hasPreview ? previewHtml : previewDescription;
let html = '';

if (full) {
html = mainHtml
} else {
if (hasPreview) {
html = previewHtml
} else {
html = previewDescription;
}
}

return (
<div className='blog-post' key={post.id}>
Expand All @@ -70,9 +74,7 @@ const Post = ({ post, full }) => {
category !== "_" && (
<span className='blog-post-category' key={category}>
<a
href={`/blog/categories/${category
.toLowerCase()
.replace(" ", "-")}`}
href={`/blog/categories/${slugify(category, {lower: true})}`}
>
{category}
</a>
Expand Down
8 changes: 4 additions & 4 deletions src/common/blog-post/PostCard.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from "react";
import { format, parseISO } from "date-fns";
import slugify from "slugify";

import parseHtml from "../../../scripts/parse-html";
import { createTags } from "../../utils/createTags";

const formatPostDate = (post) =>
format(parseISO(post.frontmatter.date), "MMMM d, yyyy");
Expand Down Expand Up @@ -41,17 +41,17 @@ const PostCard = ({ post, full }) => {
{categories.length > 0 && (
<div className='blog-post-categories'>
{categories.map(
(category, i) =>
(category, _) =>
category !== "_" && (
<span className='blog-post-category' key={category}>
<a href={`/blog/categories/${createTags(category)}`}>
<a className="blog-post-category-link" href={`/blog/categories/${slugify(category, {lower: true})}`}>
{category}
</a>
</span>
)
)}
{url && (
<span className='blog-post-category'>
<span className='blog-post-category blog-post-category-link'>
External
</span>
)}
Expand Down
14 changes: 7 additions & 7 deletions src/stylesheets/blog.scss
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,20 @@ $tablet: "screen and (max-width : 900px)";
&:last-child {
margin-right: 0;
}

a {
}

.blog-post-category-link {
display: inline-block;
padding: 10px 12px;
border-radius: 18px;
color: #999999;
background-color: #f7f7f7;
text-decoration: none;
}

&:hover {
color: #9c9c9c;
background-color: rgb(231, 231, 231);
}
}
a.blog-post-category-link:hover {
color: #9c9c9c;
background-color: rgb(231, 231, 231);
}
}

Expand Down
5 changes: 0 additions & 5 deletions src/utils/createTags.js

This file was deleted.

0 comments on commit cfced91

Please sign in to comment.