From ef3f1a3188340425d569213a730755acb9a49ac4 Mon Sep 17 00:00:00 2001 From: PhilKunz Date: Sun, 13 Nov 2016 23:11:49 +0100 Subject: [PATCH] initial --- .gitignore | 4 +++ .gitlab-ci.yml | 59 ++++++++++++++++++++++++++++++++++++++++++ README.md | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ dist/index.d.ts | 27 ++++++++++++++++++++ dist/index.js | 34 +++++++++++++++++++++++++ package.json | 32 +++++++++++++++++++++++ test/test.d.ts | 1 + test/test.js | 25 ++++++++++++++++++ test/test.ts | 25 ++++++++++++++++++ ts/index.ts | 53 ++++++++++++++++++++++++++++++++++++++ tslint.json | 3 +++ 11 files changed, 331 insertions(+) create mode 100644 .gitignore create mode 100644 .gitlab-ci.yml create mode 100644 README.md create mode 100644 dist/index.d.ts create mode 100644 dist/index.js create mode 100644 package.json create mode 100644 test/test.d.ts create mode 100644 test/test.js create mode 100644 test/test.ts create mode 100644 ts/index.ts create mode 100644 tslint.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3f93687 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +node_modules/ +coverage/ +public/ +pages/ diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..f2d2ff7 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,59 @@ +image: hosttoday/ht-docker-node:npmts + +stages: +- test +- release +- trigger +- pages + +testLEGACY: + stage: test + script: + - npmci test legacy + tags: + - docker + allow_failure: true + +testLTS: + stage: test + script: + - npmci test lts + tags: + - docker + +testSTABLE: + stage: test + script: + - npmci test stable + tags: + - docker + +release: + stage: release + script: + - npmci publish + only: + - tags + tags: + - docker + +trigger: + stage: trigger + script: + - npmci trigger + only: + - tags + tags: + - docker + +pages: + image: hosttoday/ht-docker-node:npmpage + stage: pages + script: + - npmci command npmpage --host gitlab + only: + - tags + artifacts: + expire_in: 1 week + paths: + - public diff --git a/README.md b/README.md new file mode 100644 index 0000000..b7f6f13 --- /dev/null +++ b/README.md @@ -0,0 +1,68 @@ +# smartfm +smartfm handles frontmatter of files + +## Availabililty +[![npm](https://push.rocks/assets/repo-button-npm.svg)](https://www.npmjs.com/package/smartfm) +[![git](https://push.rocks/assets/repo-button-git.svg)](https://gitlab.com/pushrocks/smartfm) +[![git](https://push.rocks/assets/repo-button-mirror.svg)](https://github.com/pushrocks/smartfm) +[![docs](https://push.rocks/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/smartfm/) + +## Status for master +[![build status](https://gitlab.com/pushrocks/smartfm/badges/master/build.svg)](https://gitlab.com/pushrocks/smartfm/commits/master) +[![coverage report](https://gitlab.com/pushrocks/smartfm/badges/master/coverage.svg)](https://gitlab.com/pushrocks/smartfm/commits/master) +[![Dependency Status](https://david-dm.org/pushrocks/smartfm.svg)](https://david-dm.org/pushrocks/smartfm) +[![bitHound Dependencies](https://www.bithound.io/github/pushrocks/smartfm/badges/dependencies.svg)](https://www.bithound.io/github/pushrocks/smartfm/master/dependencies/npm) +[![bitHound Code](https://www.bithound.io/github/pushrocks/smartfm/badges/code.svg)](https://www.bithound.io/github/pushrocks/smartfm) +[![TypeScript](https://img.shields.io/badge/TypeScript-2.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/) +[![node](https://img.shields.io/badge/node->=%206.x.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/) +[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/) + +## Usage + +say you have the following markdown file: +```markdown +--- +title: A really awesome article +date: 23-10-2020 +type: feature +--- +# A Awesome Title +The world is cool. And here is why +* reason 1 +* reason 2 +``` + +```javascript +import * as smartfm from * smartfm + +let markdownfile = `--- +testKey: testValue +testKey2: testValue2 +--- +# some markdown` + +// easy methods +let parsedData = smartfm.parse(markdownfile) + +// parsedData will be object +/* + { + data: { + testKey: testValue, + testKey2: testValue2 + }, + content: '# some markdown', + orig: '---\ntestKey: testValue\ntestKey2: testValue2\n---\n# some markdown' + } +*/ + +let newFmString = smartfm.stringify('My awesome string', {testKey1: testValue1}) +// newFmString will be '---\testKey1: testValue1\n---\nMyawesomeString' + +# class Smartfm +let mySmartfm = new smartfm.Smartfm({ + fmType: 'yaml' // can be yaml or json atm +}) +``` + +[![npm](https://push.rocks/assets/repo-header.svg)](https://push.rocks) diff --git a/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 0000000..50f309a --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1,27 @@ +import 'typings-global'; +export interface IParsedFM { + data: any; + content: string; + orig: string; +} +/** + * class smartfm handles frontmatter + */ +export declare class Smartfm { + /** + * add frontmatter to a string + */ + stringify(bodyString: string, frontmatterData: any): void; + /** + * parse a string that has frontmatter attached, YAML notation + */ + parse(stringToParse: string): IParsedFM; +} +/** + * parse a string that has frontmatter attached, YAML notation + */ +export declare let parse: (stringToParse: string) => IParsedFM; +/** + * add frontmatter to a string + */ +export declare let stringify: (bodyString: string, frontmatterData: any) => void; diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..c628884 --- /dev/null +++ b/dist/index.js @@ -0,0 +1,34 @@ +"use strict"; +require("typings-global"); +let grayMatter = require('gray-matter'); +/** + * class smartfm handles frontmatter + */ +class Smartfm { + /** + * add frontmatter to a string + */ + stringify(bodyString, frontmatterData) { + return exports.stringify(bodyString, frontmatterData); + } + /** + * parse a string that has frontmatter attached, YAML notation + */ + parse(stringToParse) { + return exports.parse(stringToParse); + } +} +exports.Smartfm = Smartfm; +/** + * parse a string that has frontmatter attached, YAML notation + */ +exports.parse = (stringToParse) => { + return grayMatter(stringToParse); +}; +/** + * add frontmatter to a string + */ +exports.stringify = (bodyString, frontmatterData) => { + grayMatter.stringify(bodyString, frontmatterData); +}; +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsMEJBQXVCO0FBQ3ZCLElBQUksVUFBVSxHQUFHLE9BQU8sQ0FBQyxhQUFhLENBQUMsQ0FBQTtBQVF2Qzs7R0FFRztBQUNIO0lBQ0k7O09BRUc7SUFDSCxTQUFTLENBQUMsVUFBa0IsRUFBRSxlQUFvQjtRQUM5QyxNQUFNLENBQUMsaUJBQVMsQ0FBQyxVQUFVLEVBQUUsZUFBZSxDQUFDLENBQUE7SUFDakQsQ0FBQztJQUVEOztPQUVHO0lBQ0gsS0FBSyxDQUFDLGFBQXFCO1FBQ3ZCLE1BQU0sQ0FBQyxhQUFLLENBQUMsYUFBYSxDQUFDLENBQUE7SUFDL0IsQ0FBQztDQUNKO0FBZEQsMEJBY0M7QUFFRDs7R0FFRztBQUNRLFFBQUEsS0FBSyxHQUFHLENBQUMsYUFBcUI7SUFDckMsTUFBTSxDQUFDLFVBQVUsQ0FBQyxhQUFhLENBQUMsQ0FBQTtBQUNwQyxDQUFDLENBQUE7QUFFRDs7R0FFRztBQUNRLFFBQUEsU0FBUyxHQUFHLENBQUMsVUFBa0IsRUFBRSxlQUFvQjtJQUM1RCxVQUFVLENBQUMsU0FBUyxDQUFDLFVBQVUsRUFBRSxlQUFlLENBQUMsQ0FBQTtBQUNyRCxDQUFDLENBQUEifQ== \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..d54a015 --- /dev/null +++ b/package.json @@ -0,0 +1,32 @@ +{ + "name": "smartfm", + "version": "1.0.0", + "description": "frontmatter done right", + "main": "dist/index.js", + "typings": "dist/index.d.ts", + "scripts": { + "test": "(npmts)" + }, + "repository": { + "type": "git", + "url": "git+ssh://git@gitlab.com/pushrocks/smartfm.git" + }, + "keywords": [ + "frontmatter" + ], + "author": "Lossless GmbH", + "license": "MIT", + "bugs": { + "url": "https://gitlab.com/pushrocks/smartfm/issues" + }, + "homepage": "https://gitlab.com/pushrocks/smartfm#README", + "dependencies": { + "gray-matter": "^2.1.0", + "typings-global": "^1.0.14" + }, + "devDependencies": { + "@types/should": "^8.1.30", + "should": "^11.1.1", + "typings-test": "^1.0.3" + } +} diff --git a/test/test.d.ts b/test/test.d.ts new file mode 100644 index 0000000..2fd432a --- /dev/null +++ b/test/test.d.ts @@ -0,0 +1 @@ +import 'typings-test'; diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..304e690 --- /dev/null +++ b/test/test.js @@ -0,0 +1,25 @@ +"use strict"; +require("typings-test"); +const should = require("should"); +const smartfm = require("../dist/index"); +describe('smartfm', function () { + let testSmartfm = new smartfm.Smartfm; + it('.parse()', function () { + let testString = `--- +testKey: testValue +testKey2: testValue2 +--- +# some markdown`; + let parsedString = testSmartfm.parse(testString); + should(parsedString.data).have.property('testKey', 'testValue'); + should(parsedString.data).have.property('testKey2', 'testValue2'); + should(parsedString.orig).equal(testString); + }); + it('.stringify', function () { + let testStringPure = `# some markdown heading +some first row`; + let testStringCombined = testSmartfm.stringify(testStringPure, { testData: 'hi' }); + console.log(testStringCombined); + }); +}); +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFxQjtBQUNyQixpQ0FBZ0M7QUFFaEMseUNBQXdDO0FBRXhDLFFBQVEsQ0FBQyxTQUFTLEVBQUU7SUFDaEIsSUFBSSxXQUFXLEdBQUcsSUFBSSxPQUFPLENBQUMsT0FBTyxDQUFBO0lBQ3JDLEVBQUUsQ0FBQyxVQUFVLEVBQUU7UUFDWCxJQUFJLFVBQVUsR0FBRzs7OztnQkFJVCxDQUFBO1FBQ1IsSUFBSSxZQUFZLEdBQUcsV0FBVyxDQUFDLEtBQUssQ0FBQyxVQUFVLENBQUMsQ0FBQTtRQUNoRCxNQUFNLENBQUMsWUFBWSxDQUFDLElBQUksQ0FBQyxDQUFDLElBQUksQ0FBQyxRQUFRLENBQUMsU0FBUyxFQUFDLFdBQVcsQ0FBQyxDQUFBO1FBQzlELE1BQU0sQ0FBQyxZQUFZLENBQUMsSUFBSSxDQUFDLENBQUMsSUFBSSxDQUFDLFFBQVEsQ0FBQyxVQUFVLEVBQUMsWUFBWSxDQUFDLENBQUE7UUFDaEUsTUFBTSxDQUFDLFlBQVksQ0FBQyxJQUFJLENBQUMsQ0FBQyxLQUFLLENBQUMsVUFBVSxDQUFDLENBQUE7SUFDL0MsQ0FBQyxDQUFDLENBQUE7SUFDRixFQUFFLENBQUMsWUFBWSxFQUFFO1FBQ2IsSUFBSSxjQUFjLEdBQUc7ZUFDZCxDQUFBO1FBQ1AsSUFBSSxrQkFBa0IsR0FBRyxXQUFXLENBQUMsU0FBUyxDQUFDLGNBQWMsRUFBRSxFQUFDLFFBQVEsRUFBRSxJQUFJLEVBQUMsQ0FBQyxDQUFBO1FBQ2hGLE9BQU8sQ0FBQyxHQUFHLENBQUMsa0JBQWtCLENBQUMsQ0FBQTtJQUNuQyxDQUFDLENBQUMsQ0FBQTtBQUNOLENBQUMsQ0FBQyxDQUFBIn0= \ No newline at end of file diff --git a/test/test.ts b/test/test.ts new file mode 100644 index 0000000..7f2a339 --- /dev/null +++ b/test/test.ts @@ -0,0 +1,25 @@ +import 'typings-test' +import * as should from 'should' + +import * as smartfm from '../dist/index' + +describe('smartfm', function() { + let testSmartfm = new smartfm.Smartfm + it('.parse()', function() { + let testString = `--- +testKey: testValue +testKey2: testValue2 +--- +# some markdown` + let parsedString = testSmartfm.parse(testString) + should(parsedString.data).have.property('testKey','testValue') + should(parsedString.data).have.property('testKey2','testValue2') + should(parsedString.orig).equal(testString) + }) + it('.stringify', function(){ + let testStringPure = `# some markdown heading +some first row` + let testStringCombined = testSmartfm.stringify(testStringPure, {testData: 'hi'}) + console.log(testStringCombined) + }) +}) diff --git a/ts/index.ts b/ts/index.ts new file mode 100644 index 0000000..20a59b4 --- /dev/null +++ b/ts/index.ts @@ -0,0 +1,53 @@ +import 'typings-global' +let grayMatter = require('gray-matter') + +export type TFrontMatter = 'yaml' | 'json' + +export interface IParsedFM { + data: any + content: string + orig: string +} + +export interface ISmartfmContructorOptions { + fmType: TFrontMatter +} + +/** + * class smartfm handles frontmatter + */ +export class Smartfm { + fmType: TFrontMatter + + constructor(optionsArg: ISmartfmContructorOptions) { + this.fmType = optionsArg.fmType + } + + /** + * add frontmatter to a string + */ + stringify(bodyString: string, frontmatterData: any) { + return stringify(bodyString, frontmatterData) + } + + /** + * parse a string that has frontmatter attached, YAML notation + */ + parse(stringToParse: string): IParsedFM { + return parse(stringToParse) + } +} + +/** + * parse a string that has frontmatter attached, YAML notation + */ +export let parse = (stringToParse: string): IParsedFM => { + return grayMatter(stringToParse) +} + +/** + * add frontmatter to a string + */ +export let stringify = (bodyString: string, frontmatterData: any) => { + grayMatter.stringify(bodyString, frontmatterData) +} diff --git a/tslint.json b/tslint.json new file mode 100644 index 0000000..45052ad --- /dev/null +++ b/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "tslint-config-standard" +}