From 8882beb55d6456d32a3240b657bfeb0ad6ef13ad Mon Sep 17 00:00:00 2001 From: nikaera Date: Sat, 9 May 2020 23:19:19 +0900 Subject: [PATCH] Add indentation according to the type of h tag --- backend/app.js | 1 + backend/lib/create-toc.js | 2 ++ src/display/Display.js | 3 +- src/toc/MultiLevelTOC.js | 63 ++++++++++++++++++++++++++++++++++++ src/toc/MultiLevelTOCItem.js | 30 +++++++++++++++++ 5 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 src/toc/MultiLevelTOC.js create mode 100644 src/toc/MultiLevelTOCItem.js diff --git a/backend/app.js b/backend/app.js index 181ddf8..9f9fb06 100644 --- a/backend/app.js +++ b/backend/app.js @@ -15,6 +15,7 @@ app.get('/', (req, res) => { }) }) .catch(error => { + console.error(error) res.sendStatus(502) }) diff --git a/backend/lib/create-toc.js b/backend/lib/create-toc.js index 328d5b3..8943170 100644 --- a/backend/lib/create-toc.js +++ b/backend/lib/create-toc.js @@ -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; @@ -23,6 +24,7 @@ function createTOC(uri) { const link = `https://medium.com/p/${articleId}#${headingCode}`; results.push({ + tagNum, title, headingCode, link diff --git a/src/display/Display.js b/src/display/Display.js index 0088229..628f20a 100644 --- a/src/display/Display.js +++ b/src/display/Display.js @@ -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}) => { diff --git a/src/toc/MultiLevelTOC.js b/src/toc/MultiLevelTOC.js new file mode 100644 index 0000000..c1feca6 --- /dev/null +++ b/src/toc/MultiLevelTOC.js @@ -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 ( + + ) + } + return ( +
+

Table of Contents

+ +
+ ); + } + + 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; diff --git a/src/toc/MultiLevelTOCItem.js b/src/toc/MultiLevelTOCItem.js new file mode 100644 index 0000000..2c1083a --- /dev/null +++ b/src/toc/MultiLevelTOCItem.js @@ -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;