Skip to content

Commit

Permalink
Merge pull request #10 from technote-space/feature/#9
Browse files Browse the repository at this point in the history
feat: add title setting (#9)
  • Loading branch information
technote-space authored Aug 31, 2019
2 parents 119ea80 + 45f2cea commit 7805a19
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: TOC Generator
uses: technote-space/toc-generator@v0
uses: technote-space/toc-generator@v1
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TARGET_PATHS: 'README.md,.github/CODE_OF_CONDUCT.md,.github/CONTRIBUTING.md'
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Just run [DocToc](https://github.com/thlorenz/doctoc) and commit to branch if ch
- [Installation](#installation)
- [Options](#options)
- [TARGET_PATHS](#target_paths)
- [TOC_TITLE](#toc_title)
- [COMMIT_MESSAGE](#commit_message)
- [Action event details](#action-event-details)
- [Target event](#target-event)
Expand Down Expand Up @@ -50,6 +51,9 @@ Just run [DocToc](https://github.com/thlorenz/doctoc) and commit to branch if ch
### TARGET_PATHS
Target file path. (Comma separated, [Detail](https://github.com/thlorenz/doctoc#adding-toc-to-individual-files))
default: `'README.md'`
### TOC_TITLE
TOC Title.
default: `'**Table of Contents**'`
### COMMIT_MESSAGE
Commit message.
default: `'docs: Update TOC'`
Expand Down
13 changes: 7 additions & 6 deletions __tests__/utils/misc.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path';
import {getContext, getApiFixture, disableNetConnect, testEnv} from '../util';
import {getContext, testEnv} from '../util';
import {
isTargetEvent,
getBuildVersion,
Expand Down Expand Up @@ -66,15 +66,16 @@ describe('getBuildVersion', () => {
describe('getDocTocArgs', () => {
testEnv();

it('should get default DocToc args', () => {
it('should get DocToc args', () => {
process.env.GITHUB_WORKSPACE = '/tmp/workspace';
expect(getDocTocArgs()).toBe('/tmp/workspace/.work/README.md');
process.env.INPUT_TARGET_PATHS = 'README.md,.github/CONTRIBUTING.md';
process.env.INPUT_TOC_TITLE = '**Table of Contents**';
expect(getDocTocArgs()).toBe('/tmp/workspace/.work/README.md /tmp/workspace/.work/.github/CONTRIBUTING.md --title **Table of Contents**');
});

it('should get DocToc args', () => {
it('should get default DocToc args', () => {
process.env.GITHUB_WORKSPACE = '/tmp/workspace';
process.env.INPUT_TARGET_PATHS = 'README.md,.github/CONTRIBUTING.md';
expect(getDocTocArgs()).toBe('/tmp/workspace/.work/README.md /tmp/workspace/.work/.github/CONTRIBUTING.md');
expect(getDocTocArgs()).toBe('/tmp/workspace/.work/README.md --notitle');
});
});

Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ inputs:
TARGET_PATHS:
description: Target file path. (Comma separated, @see https://github.com/thlorenz/doctoc#adding-toc-to-individual-files)
default: 'README.md'
TOC_TITLE:
description: TOC Title
default: '**Table of Contents**'
COMMIT_MESSAGE:
description: Commit message.
default: 'docs: Update TOC'
Expand Down
2 changes: 1 addition & 1 deletion src/utils/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const getCurrentBranchName = async (workDir: string): Promise<string> => {
const runDocToc = async (workDir: string): Promise<boolean> => {
const args = getDocTocArgs();
const doctoc = path.resolve(workDir, 'node_modules/.bin/doctoc');
await execAsync(`yarn --cwd ${workDir} add doctoc`);
await execAsync(`yarn --cwd ${workDir} add doctoc`, false, null, false, true);
await execAsync(`${doctoc} ${args} --github`);
return true;
};
Expand Down
5 changes: 4 additions & 1 deletion src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ const getTargetPaths = (): string[] => [...new Set<string>((getInput('TARGET_PAT

export const getDocTocArgs = () => {
const workDir = getWorkDir();
return getTargetPaths().map(item => path.resolve(workDir, item)).join(' ');
const title = getTocTitle().replace('\'', '\\\'').replace('"', '\\"');
return getTargetPaths().map(item => path.resolve(workDir, item)).join(' ') + (title ? ` --title ${title}` : ' --notitle');
};

const getTocTitle = (): string => getInput('TOC_TITLE') || '';

const getWorkspace = (): string => process.env.GITHUB_WORKSPACE || '';

export const getWorkDir = () => path.resolve(getWorkspace(), '.work');
Expand Down

0 comments on commit 7805a19

Please sign in to comment.