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 indentation according to the type of h tag #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ app.get('/', (req, res) => {
})
})
.catch(error => {
console.error(error)
res.sendStatus(502)
})

Expand Down
2 changes: 2 additions & 0 deletions backend/lib/create-toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function createTOC(uri) {
.each((_, elem) => {
const title = elem.children[0].data;
const headingCode = elem.attribs.id;
const tagNum = parseInt(elem.name.slice(-1));

if (!headingCode) {
return;
Expand All @@ -23,6 +24,7 @@ function createTOC(uri) {
const link = `https://medium.com/p/${articleId}#${headingCode}`;

results.push({
tagNum,
title,
headingCode,
link
Expand Down
3 changes: 2 additions & 1 deletion src/display/Display.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import TOC from "../toc/TOC";
// import TOC from "../toc/TOC";
import TOC from "../toc/MultiLevelTOC";
import UrlForm from "../form/Form";

const Display = ({results, handleSubmit}) => {
Expand Down
63 changes: 63 additions & 0 deletions src/toc/MultiLevelTOC.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React, { Component } from "react";
import MultiLevelTOCItem from "./MultiLevelTOCItem";
import "./TOC.css";

class MultiLevelTOC extends Component {
render() {
const nestedList = this.formatData(this.props.results)
const createNestedListElement = (key, list) => {
return (
<ul key={key}>
{list.map((l, i) => {
if(Array.isArray(l)) {
return createNestedListElement(`${key}-${i}`, l)
} else {
return <li key={`${key}-${i}-${l.id}`}><a href={l.link}>{l.title}</a></li>;
}
})}
</ul>
)
}
return (
<div className="results">
<h4>Table of Contents</h4>
<ul>
{createNestedListElement("root", nestedList)}
</ul>
</div>
);
}

formatData(results) {
const rootItem = new MultiLevelTOCItem({});
rootItem.id = "root";

var currentId = rootItem.id
var currentTagNum = 1

const parentIdQueue = [rootItem.id]
const items = []

for(const result of results) {
if(currentTagNum < result.tagNum) {
parentIdQueue.push(currentId)
} else if(currentTagNum > result.tagNum) {
const removeTimes = currentTagNum - result.tagNum
for(var i = 0; i < removeTimes; i++) {
parentIdQueue.pop()
}
}

const parentId = parentIdQueue.slice(-1)[0]
const item = new MultiLevelTOCItem(result, parentId)
items.push(item)

currentId = item.id
currentTagNum = result.tagNum
}

return rootItem.nestedList(rootItem.id, items);
}
}

export default MultiLevelTOC;
30 changes: 30 additions & 0 deletions src/toc/MultiLevelTOCItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class MultiLevelTOCItem {
constructor(result, parentId) {
this.id = result.headingCode;
this.parentId = parentId;
this.title = result.title
this.link = result.link
}

childrenOf(targetId, items) {
const children = []
for(const item of items) {
if (item.parentId === targetId) {
children.push(item)
}
}
return children
}
}
MultiLevelTOCItem.prototype.nestedList = function(id, items) {
const children = this.childrenOf(id, items)
return children.map((child) => {
const childItems = this.childrenOf(child.id, items)
if(childItems.length > 0) {
return this.nestedList(child.id, items)
}
return child
})
}

export default MultiLevelTOCItem;