Skip to content
Merged
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules/
lib/
.mocharc.yml
*.pdf
test/*.actual
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Usage: pandiff [OPTIONS] FILE1 FILE2
-t, --to=FORMAT
-v, --version
--wrap=STRING
--metadata
```

### Git integration
Expand Down
1 change: 1 addition & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const optionDefinitions: commandLineArgs.OptionDefinition[] = [
{name: 'version', alias: 'v', type: Boolean},
{name: 'wrap', type: String},
{name: 'files', multiple: true, defaultOption: true},
{name: 'metadata', type: String},
];

function help() {
Expand Down
81 changes: 81 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,84 @@ describe('Misc', () => {
expectWithFallback(output, expected, 'test/diff-table.html');
});
});

describe('Metadata', () => {
it('keep metadata from old document', async () => {
const output = await pandiff(
'test/old-metadata.md',
'test/new-metadata.md',
{
files: true,
metadata: 'old',
to: 'markdown',
}
);
const expected = fs.readFileSync('test/keep-old-metadata-diff.md', 'utf8');
expect(output, expected);
});
it('keep metadata from new document', async () => {
const output = await pandiff(
'test/old-metadata.md',
'test/new-metadata.md',
{
files: true,
metadata: 'new',
to: 'markdown',
}
);
const expected = fs.readFileSync('test/keep-new-metadata-diff.md', 'utf8');
expect(output, expected);
});
it('keep metadata from no document', async () => {
const output = await pandiff(
'test/old-metadata.md',
'test/new-metadata.md',
{
files: true,
metadata: 'none',
to: 'markdown',
}
);
const expected = fs.readFileSync('test/keep-no-metadata-diff.md', 'utf8');
expect(output, expected);
});
it('keep metadata from old document without metadata', async () => {
const output = await pandiff('test/old.md', 'test/new.md', {
files: true,
metadata: 'old',
to: 'markdown',
});
const expected = fs.readFileSync('test/diff.md', 'utf8');
expect(output, expected);
});
it('keep metadata from new document without metadata', async () => {
const output = await pandiff('test/old.md', 'test/new.md', {
files: true,
metadata: 'new',
to: 'markdown',
});
const expected = fs.readFileSync('test/diff.md', 'utf8');
expect(output, expected);
});
it('keep metadata none document without metadata', async () => {
const output = await pandiff('test/old.md', 'test/new.md', {
files: true,
metadata: 'none',
to: 'markdown',
});
const expected = fs.readFileSync('test/diff.md', 'utf8');
expect(output, expected);
});
it('keep no metadata if metadata is not specified', async () => {
const output = await pandiff(
'test/old-metadata.md',
'test/new-metadata.md',
{
files: true,
to: 'markdown',
}
);
const expected = fs.readFileSync('test/keep-no-metadata-diff.md', 'utf8');
expect(output, expected);
});
});
50 changes: 42 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,38 @@ async function convert(source: string, opts: pandiff.Options = {}) {
return html;
}

async function extractMetadata(source: string) {
const file = fs.readFileSync(source, 'utf8');
const lines = file.split('\n');
if (lines[0].trim() !== '---') return '';

const metadata = [lines[0]];
for (let i = 1; i < lines.length; i++) {
metadata.push(lines[i]);
if (lines[i].trim() === '---') break;
}

return metadata.join('\n') + '\n';
}

async function pandiff(
source1: string,
source2: string,
opts: pandiff.Options = {}
): Promise<string | null> {
let metadata = '';
switch (opts.metadata) {
case 'old':
opts.standalone = true;
metadata = await extractMetadata(source1);
break;
case 'new':
opts.standalone = true;
metadata = await extractMetadata(source2);
break;
default:
break;
}
const html1 = await convert(source1, opts);
const html2 = await convert(source2, opts);
const html = htmldiff(html1, html2);
Expand All @@ -282,7 +309,7 @@ async function pandiff(
);
return null;
} else {
return render(html, opts);
return render(html, opts, metadata);
}
}

Expand Down Expand Up @@ -319,7 +346,7 @@ const regex = {
},
};

async function render(html: string, opts: pandiff.Options = {}) {
async function render(html: string, opts: pandiff.Options = {}, metadata = '') {
html = postprocess(html);
const args = buildArgs(opts, 'reference-links');
args.push('--wrap=none');
Expand Down Expand Up @@ -357,7 +384,7 @@ async function render(html: string, opts: pandiff.Options = {}) {
}
}
const text = lines.join('\n');
return postrender(text, opts);
return postrender(text, opts, metadata);
}

const criticHTML = (text: string) =>
Expand Down Expand Up @@ -411,8 +438,12 @@ const pandocOptionsHTML = [
'--standalone',
];

async function postrender(text: string, opts: pandiff.Options = {}) {
if (!opts.output && !opts.to) return text;
async function postrender(
text: string,
opts: pandiff.Options = {},
metadata = ''
) {
if (!opts.output && !opts.to) return `${metadata}${text}`;

if (!('highlight-style' in opts)) opts['highlight-style'] = 'kate';
let args = buildArgs(
Expand Down Expand Up @@ -446,19 +477,21 @@ async function postrender(text: string, opts: pandiff.Options = {}) {
if (opts.standalone) args = args.concat(pandocOptionsHTML);
}

if (opts.standalone) args = args.concat('-s');

if (opts.output) {
await sh.pandoc(...args).end(text);
await sh.pandoc(...args).end(`${metadata}${text}`);
return null;
} else {
return sh
.pandoc(...args)
.end(text)
.end(`${metadata}${text}`)
.toString();
}
}

export = pandiff;
namespace pandiff { // eslint-disable-line
namespace pandiff { // eslint-disable-line
export type File = string;
export type Format = string;
export type Path = string;
Expand All @@ -484,6 +517,7 @@ namespace pandiff { // eslint-disable-line
version?: boolean;
wrap?: 'auto' | 'none' | 'preserve';
files?: boolean;
metadata?: 'new' | 'old' | 'none';
}
/* eslint-disable no-inner-declarations */
export async function trackChanges(
Expand Down
15 changes: 15 additions & 0 deletions test/keep-new-metadata-diff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: New Title
---

# {\~~Some~\>Differnt\~\~} Text

Lorem {--ipsum --}dolor sit {~~amet,\~\>amet test,~~} consetetur
sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et
dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et
justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea
takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit
amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At
vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd
gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
11 changes: 11 additions & 0 deletions test/keep-no-metadata-diff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# {\~~Some~\>Differnt\~\~} Text

Lorem {--ipsum --}dolor sit {~~amet,\~\>amet test,~~} consetetur
sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et
dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et
justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea
takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit
amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At
vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd
gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
15 changes: 15 additions & 0 deletions test/keep-old-metadata-diff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: Old Title
---

# {\~~Some~\>Differnt\~\~} Text

Lorem {--ipsum --}dolor sit {~~amet,\~\>amet test,~~} consetetur
sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et
dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et
justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea
takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit
amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At
vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd
gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
6 changes: 6 additions & 0 deletions test/new-metadata.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: New Title
---

# Differnt Text
Lorem dolor sit amet test, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
6 changes: 6 additions & 0 deletions test/old-metadata.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Old Title
---

# Some Text
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
Loading