Skip to content

Commit

Permalink
✨ Markdown Node
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoE105 committed May 2, 2021
1 parent 7f0f8de commit 318f5cf
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 0 deletions.
181 changes: 181 additions & 0 deletions packages/nodes-base/nodes/Markdown/Markdown.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import {
IExecuteFunctions,
} from 'n8n-core';

import {
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';

//@ts-expect-error
import * as showdown from 'showdown';

//@ts-expect-error
import * as jsdom from 'jsdom';

const dom = new jsdom.JSDOM();

const converter = new showdown.Converter();

import {
set,
} from 'lodash';

export class Markdown implements INodeType {
description: INodeTypeDescription = {
displayName: 'Markdown',
name: 'markdown',
icon: 'file:markdown.svg',
group: ['output'],
version: 1,
subtitle: '={{$parameter["mode"]==="markdownToHtml" ? "Markdown to HTML" : "HTML to Markdown"}}',
description: 'Move data between Markdown and HTML.',
defaults: {
name: 'Markdown',
color: '#000000',
},
inputs: ['main'],
outputs: ['main'],
credentials: [],
properties: [
{
displayName: 'Mode',
name: 'mode',
type: 'options',
options: [
{
name: 'Markdown to HTML',
value: 'markdownToHtml',
description: 'Move data from MarkdoWN to HTML',
},
{
name: 'HTML to Markdown',
value: 'htmlToMarkdown',
description: 'Move data from HTML to Markdown.',
},
],
default: 'htmlToMarkdown',
description: 'From and to where data should be moved.',
},
{
displayName: 'HTML',
name: 'html',
type: 'string',
displayOptions: {
show: {
mode: [
'htmlToMarkdown',
],
},
},
default: '',
required: true,
description: 'The HTML to be converted.',
},
{
displayName: 'Markdown',
name: 'markdown',
type: 'string',
displayOptions: {
show: {
mode: [
'markdownToHtml',
],
},
},
default: '',
required: true,
description: 'The Markdown to be converted.',
},
{
displayName: 'Destination Key',
name: 'destinationKey',
type: 'string',
displayOptions: {
show: {
mode: [
'markdownToHtml',
'htmlToMarkdown',
],
},
},
default: 'data',
required: true,
placeholder: '',
description: 'The name the JSON key to copy data to. It is also possible<br />to define deep keys by using dot-notation like for example:<br />"level1.level2.newKey"',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
default: {},
options: [
{
displayName: 'Flavor',
name: 'flavor',
type: 'options',
options: [
{
name: 'Github',
value: 'github',
description: 'GFM (GitHub Flavored Markdown)',
},
{
name: 'Vanilla',
value: 'vanilla',
description: 'Showdown base flavor',
},
{
name: 'Original',
value: 'original',
description: `Original markdown flavor as in <a href="https://daringfireball.net/projects/markdown/" rel="nofollow">John Gruber's spec</ a >`,
},
],
displayOptions: {
show: {
'/mode': [
'htmlToMarkdown',
],
},
},
default: 'vanilla',
},
],
},
],
};

async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: INodeExecutionData[] = [];
const length = items.length as unknown as number;
let item: INodeExecutionData;
const mode = this.getNodeParameter('mode', 0) as string;

for (let i = 0; i < length; i++) {
if (mode === 'htmlToMarkdown') {
const options = this.getNodeParameter('options', i) as IDataObject;
const destinationKey = this.getNodeParameter('destinationKey', i) as string;
converter.setFlavor(options.flavor as showdown.Flavor);
const html = this.getNodeParameter('html', i) as string;
const md = converter.makeMarkdown(html, dom.window.document);
item = { ...items[i] } as INodeExecutionData;
set(item.json, destinationKey, md);
returnData.push(item);
}

if (mode === 'markdownToHtml') {
const markdown = this.getNodeParameter('markdown', i) as string;
const html = converter.makeHtml(markdown);
const destinationKey = this.getNodeParameter('destinationKey', i) as string;
item = { ...items[i] } as INodeExecutionData;
set(item.json, destinationKey, html);
returnData.push(item);
}
}
return this.prepareOutputData(returnData);
}
}
6 changes: 6 additions & 0 deletions packages/nodes-base/nodes/Markdown/markdown.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions packages/nodes-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@
"dist/nodes/Mailjet/Mailjet.node.js",
"dist/nodes/Mailjet/MailjetTrigger.node.js",
"dist/nodes/Mandrill/Mandrill.node.js",
"dist/nodes/Markdown/Markdown.node.js",
"dist/nodes/Matrix/Matrix.node.js",
"dist/nodes/Mattermost/Mattermost.node.js",
"dist/nodes/Mautic/Mautic.node.js",
Expand Down Expand Up @@ -607,6 +608,7 @@
"iconv-lite": "^0.6.2",
"imap-simple": "^4.3.0",
"iso-639-1": "^2.1.3",
"jsdom": "^16.5.3",
"jsonwebtoken": "^8.5.1",
"kafkajs": "^1.14.0",
"lodash.get": "^4.4.2",
Expand All @@ -630,6 +632,7 @@
"request": "^2.88.2",
"rhea": "^1.0.11",
"rss-parser": "^3.7.0",
"showdown": "^1.9.1",
"snowflake-sdk": "^1.5.3",
"ssh2-sftp-client": "^5.2.1",
"tmp-promise": "^3.0.2",
Expand Down

0 comments on commit 318f5cf

Please sign in to comment.