Skip to content

Commit

Permalink
fix: bugs and dev mode
Browse files Browse the repository at this point in the history
  • Loading branch information
gadzan committed May 15, 2020
1 parent c4c3574 commit 9c5273f
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 45 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ GeneraToc generates a table of contents (TOC) from headings in an HTML document.

This library was inspired by [Tocify](https://github.com/gfranko/jquery.tocify.js) and [Tocbot](https://github.com/tscanlin/tocbot), but GeneraToc **uses native DOM** methods with **no other dependencies** and unlike Tocbot, GeneraToc does **NOT rely on id attribute** to navigate to the heading.

## DEMO

[https://gadzan.github.io/generatoc/demo/](https://gadzan.github.io/generatoc/demo/)

## Installation

Install it with npm.
```
```bash
npm install --save generatoc
```

OR Install it with yarn.

```bash
yarn add generatoc
```
Expand Down Expand Up @@ -69,6 +72,12 @@ const selector = '#toc'
generatoc.init({ content, heading, selector })
```

## Development

```bash
npm run dev
```

### One more thing

** If the `selector` is not `#toc` you have to modify css file manully **, just replace all `#toc` string with your selector name in css file which can be found at `./node_modules/generatoc/src/style/main.css`
Expand All @@ -77,6 +86,10 @@ generatoc.init({ content, heading, selector })

- [x] Destory method
- [x] Reload method
- [x] Development env
- [] Test cases
- [] Show and close animation
- [] More configuration

## LICENSE
MIT
2 changes: 1 addition & 1 deletion build/generatoc.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/generatoc.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions build/generatoc.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../build/generatoc.min.css">
<title>Document</title>
<title>GeneraToc Demo</title>
<style>
.post-content {
width: 75%;
Expand All @@ -15,6 +15,8 @@
</head>
<body>
<div class="post-content">
<button onclick="generatoc.refresh()">Refresh</button>
<button onclick="generatoc.destroy()">Destroy</button>
<h1>H1: Donald Trump's 'stupid' suggestion for fighting coronavirus prompts furious response from medical experts</h1>
<p>Donald Trump has been roundly slammed after suggesting that 'disinfectant injections' could help fight coronavirus during a bizarre White House press briefing.</p>
<h2>H2: This is the 1st H2</h2>
Expand All @@ -35,7 +37,7 @@ <h5>H5: This is the 1st H5</h5>
<p>Bryan had said the coronavirus appears to weaken more quickly when exposed to sunlight, heat and humidity, which could lead to a fall in infections as the weather improves.</p>
<h6>H6: This is the 1st H6</h6>
<p>Trump appeared to take that idea and run with it.</p>
<h2>H2: This is the 2nd H2</h2>
<h2>H2: This is the 3rd H2</h2>
<p>
"Supposing we hit the body with a tremendous... whether it's ultraviolet or just very powerful light," he asked.
</p>
Expand All @@ -56,20 +58,21 @@ <h4>H4: This is the 3rd H4</h4>
<p>
After the briefing, health experts were quick to point out that ingesting such substances would likely prove fatal.
</p>
<h2>H2: This is the 3rd H2</h2>
<h2>H2: This is the 4th H2</h2>
<p>
"This notion of injecting or ingesting any type of cleansing product into the body is irresponsible, and it's dangerous," global health policy expert Dr Vin Gupta told NBC.
</p>
<p>
He and other medical professionals reacting on social media urged people to ignore Trump's suggestions.
</p>
<h5>H3: This is the 2nd H5</h5>
<h5>H5: This is the 2nd H5</h5>
<p>
It isn't the first time the president has pushed unsubstantiated 'solutions' to coronavirus, as he previously promoted hydroxychloroquine as a possible cure - a drug that has no proven benefit in fighting covid-19.
</p>
<p>|</p>
<p>|</p>
<p>|</p>
<h5>H5: This is the 3rd H5</h5>
<p>|</p>
<p>|</p>
<p>|</p>
Expand Down
Empty file added ds
Empty file.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "generatoc",
"version": "1.0.9",
"version": "1.1.0",
"description": "Automatically generate table of content from heading of HTML document",
"main": "dist/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "rollup -c",
"dev": "rollup -w -c rollup.config.dev.js",
"release": "npm publish"
},
"repository": {
Expand Down Expand Up @@ -41,6 +42,8 @@
"postcss-preset-env": "^6.7.0",
"rollup": "^2.10.0",
"rollup-plugin-postcss": "^3.1.1",
"rollup-plugin-serve": "^1.0.1",
"rollup-plugin-sourcemaps": "^0.6.2",
"rollup-plugin-terser": "^5.3.0",
"rollup-plugin-typescript2": "^0.27.1",
"tslib": "^2.0.0",
Expand Down
29 changes: 29 additions & 0 deletions rollup.config.dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import path from 'path';
import devServe from 'rollup-plugin-serve'
import sourceMaps from "rollup-plugin-sourcemaps"
import configList from './rollup.config'

const PORT = 3030;

const devConfig = configList
.filter((_config, index) => index !== 0)
.map((config, _index) => {
config.output.sourcemap = true;
config.plugins = [
...config.plugins,
...[
devServe({
open: true,
openPage: '/demo/',
port: PORT,
contentBase: [
path.resolve(__dirname, ''),
]
})
],
sourceMaps()
]
return config;
})

export default devConfig
32 changes: 14 additions & 18 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import path from 'path';
// import typescript from '@rollup/plugin-typescript';
import typescript from 'rollup-plugin-typescript2';
import babel from '@rollup/plugin-babel'
import postcss from 'rollup-plugin-postcss';
import { terser } from "rollup-plugin-terser";

const babelOptions = {
presets: ['@babel/preset-env'],
babelHelpers: 'bundled'
}

export default [
// Output esm version to dist folder
{
input: [
path.resolve(__dirname, 'src', 'index.ts'),
],
output: [
{
file: path.resolve(__dirname, 'dist','index.js'),
format: 'esm',
},
],
output: {
file: path.resolve(__dirname, 'dist','index.js'),
format: 'esm',
},
plugins: [
typescript({
typescript: require('typescript'),
Expand All @@ -32,19 +31,18 @@ export default [
babel(babelOptions)
]
},
// Output browser version to build folder
{
input: {
'generatoc.min': path.resolve(__dirname, 'src','build.ts'),
},
output: [
{
dir: 'build',
// file: path.resolve(__dirname, 'build','generatoc.min.js'),
format: 'iife',
name: 'generatoc',
plugins: [ terser() ]
}
],
output: {
dir: 'build',
// file: path.resolve(__dirname, 'build','generatoc.min.js'),
format: 'iife',
name: 'generatoc',
plugins: [ terser() ]
},
plugins: [
typescript({
typescript: require('typescript'),
Expand All @@ -55,8 +53,6 @@ export default [
minimize: true,
}),
babel(babelOptions),
// terser()
]
}
]

3 changes: 1 addition & 2 deletions src/build.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import './style/css.ts'

import './style/main.css'
import generatoc from './index'

export default generatoc
26 changes: 12 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function getLastHeadingParentOf (level: number, headings: List[], index: number)
ele: null,
children: headings
}
while (!tmp.ele || tmp.level !== level) {
while (tmp.level !== level) {
parent = tmp
tmp = last(tmp.children)
if (typeof tmp === 'undefined') {
Expand Down Expand Up @@ -134,30 +134,27 @@ function traceParentAndShow (ele: HTMLElement) {
}
}

function getRealUl (element: HTMLElement | Element): HTMLCollection | undefined {
if (!element || !element.children[0]) {
function showRealUlChildren (element: HTMLElement | Element): HTMLCollection | undefined {
if (!element || !element.children || element.children.length === 0) {
return undefined
}
if (element.children[0].tagName.toLowerCase() === 'ul') {
Array.prototype.forEach.call(element.children, (ul: HTMLElement) => {
ul.style.display = 'block'
Array.prototype.forEach.call(element.children, (ele: HTMLElement) => {
if (ele.tagName.toLowerCase() === 'ul') {
ele.style.display = 'block'
}
})
return getRealUl(element.children[0])
return showRealUlChildren(element.children[0])
}
return element.children
(<HTMLElement>element).style.display = 'block'
}

function showEvent (e: Event) {
e.stopPropagation()
console.log(e)
// console.log(e)
hideAllTocSubHeading(document.querySelector(tocSelector)!)
const element = <HTMLElement>e.target
const uls = getRealUl(element.parentElement!.parentElement!.children[1])
if (uls) {
Array.prototype.forEach.call(uls, (ul: HTMLElement) => {
ul.style.display = 'block'
})
}
const ul = showRealUlChildren(element.parentElement!.parentElement!.children[1])
traceParentAndShow(element)
}

Expand Down Expand Up @@ -238,6 +235,7 @@ const generatoc: Generatoc = {
previousNode = index === 0 ? null : headingNode[index - 1]
processNode(hNode, previousNode, headingList, index)
})
console.log('headingList', headingList)
renderToc()
},
destroy () {
Expand Down
1 change: 0 additions & 1 deletion src/style/css.ts

This file was deleted.

2 changes: 2 additions & 0 deletions src/style/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ body {

#toc ul {
margin-left: 1em;
transition: all .2s ease-out;
}

#toc li {
display: block;
transition: all .2s ease-out;
}

@media only screen and (max-width: 540px) {
Expand Down
Loading

0 comments on commit 9c5273f

Please sign in to comment.