Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
99c73e1
Getting started React + improve snippetManager
thomasarbona May 9, 2019
db5e366
fix eslint error
thomasarbona May 9, 2019
52e23f1
flatten titles
thomasarbona May 10, 2019
67ff71c
nitpicking: avoid unnecessary indentation
thomasarbona May 10, 2019
a43484f
remove yarn dependencies
thomasarbona May 10, 2019
76127a4
use tree output to show file hierarchy
thomasarbona May 10, 2019
f58f117
add useful docs link
thomasarbona May 10, 2019
16bec59
add more context on snippet
thomasarbona May 10, 2019
52d1e0e
nitpicking: good variables naming
thomasarbona May 10, 2019
5f01cad
add reference to Kuzzle and React boilerplate
thomasarbona May 10, 2019
c27f150
fix snipper error
thomasarbona May 13, 2019
f2b8563
fix tree
thomasarbona May 13, 2019
40db49b
Getting started React + improve snippetManager
thomasarbona May 9, 2019
f1a6123
fix eslint error
thomasarbona May 9, 2019
46835c6
flatten titles
thomasarbona May 10, 2019
524abc9
nitpicking: avoid unnecessary indentation
thomasarbona May 10, 2019
2cc832e
remove yarn dependencies
thomasarbona May 10, 2019
21df728
use tree output to show file hierarchy
thomasarbona May 10, 2019
eb1deae
add useful docs link
thomasarbona May 10, 2019
0ca482f
add more context on snippet
thomasarbona May 10, 2019
affb3cb
nitpicking: good variables naming
thomasarbona May 10, 2019
8f94157
add reference to Kuzzle and React boilerplate
thomasarbona May 10, 2019
0bf4213
fix snipper error
thomasarbona May 13, 2019
2e4665b
fix tree
thomasarbona May 13, 2019
d9f7411
Merge branch '2-dev' into getting-started-react
thomasarbona May 13, 2019
d6fe8dd
put tree in bash code block
thomasarbona May 13, 2019
99887dc
merge
thomasarbona May 13, 2019
eb48d74
Merge branch '2-dev' into getting-started-react
benoitvidis May 15, 2019
77f990c
Update src/sdk-reference/js/6/getting-started/react/index.md
thomasarbona May 15, 2019
a7396ff
Update src/sdk-reference/js/6/getting-started/react/index.md
thomasarbona May 15, 2019
5bdf9e2
Update src/sdk-reference/js/6/getting-started/react/index.md
thomasarbona May 15, 2019
78260e6
Update src/sdk-reference/js/6/getting-started/react/index.md
thomasarbona May 15, 2019
7a62789
Merge branch '2-dev' into getting-started-react
scottinet May 17, 2019
636b434
Update src/sdk-reference/js/6/getting-started/react/index.md
thomasarbona May 20, 2019
dd4e505
update success template
thomasarbona May 20, 2019
790a15f
Merge branch 'getting-started-react' of https://github.com/kuzzleio/d…
thomasarbona May 20, 2019
a380b6b
Merge branch '2-dev' into getting-started-react
benoitvidis May 20, 2019
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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"express": "^4.16.4",
"fast-deepclone": "^1.0.1",
"handlebars": "^4.1.1",
"highlight.js": "^9.15.6",
"hoek": "^6.1.2",
"indent-string": "^3.2.0",
"jquery": "^3.3.1",
Expand Down
143 changes: 106 additions & 37 deletions plugins/snippetManager.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,123 @@
const
fs = require('fs'),
const fs = require('fs'),
path = require('path'),
marked = require('marked'),
MarkdownIt = require('markdown-it'),
color = require('colors/safe');
color = require('colors/safe'),
hljs = require('highlight.js'),
indentLine = require('indent-string');

const SNIPPET_REGEX = /(\[snippet=)[a-zA-Z0-9-]+\]/g;
const SNIPPET_INOTHER_REGEX = /(\[\[snippet=)[a-zA-Z0-9-]+\]]/g;

const SNIPPET_EXTRACT = /\/\* *snippet:start *\*\/\n?((.|\n)*?)\/\* *snippet:end *\*\//;

module.exports = {
const md = new MarkdownIt();

process (filename, data) {
let
self = this,
fileString = data.contents.toString(),
match = fileString.match(SNIPPET_REGEX),
presentLanguages = [],
md = new MarkdownIt();

if(match) {
match.forEach(el => {
let
name = el.split('=')[1].slice(0, -1),
fullPath = path.join(__dirname, '../src/' + filename.split('/').slice(0, -1).join('/') + '/snippets'),
code = '',
filenames = fs.readdirSync(fullPath);

filenames.forEach(file => {

if (file.split('.')[0] === name && file.substr(-8) !== 'test.yml') {
presentLanguages.push(file.split('.')[1]);
let fileContent = fs.readFileSync(fullPath + '/' + file, 'utf8');
const lng = file.split('.')[1];
const languageName = lng === 'js' ? 'javascript' : lng;

code += '``` ' + languageName + '\n' + fileContent + '\n```\n';
}

});
const markdown = md.render(code);
fileString = fileString.replace(el, markdown);
const countIndent = (string, pos) => {
let count = 0;
let idx = pos - 1;
const char = string[idx];
for (; string[idx] === ' ' || string[idx] === '\t'; idx -= 1) {
count += 1;
}
return {
char,
count
};
};

const getDirPath = filename =>
path.join(
__dirname,
'../src/' +
filename
.split('/')
.slice(0, -1)
.join('/') +
'/snippets'
);

const getMarkdown = (code, lang, indent, wrapWithTag) => {
if (wrapWithTag) {
return md.render(code);
}
const codeIndented = indentLine(code, indent.count, { indent: indent.char });
return hljs.highlight(lang, codeIndented, true).value;
};

const getSnippetContent = (dir, file) => {
let content = fs.readFileSync(dir + '/' + file, 'utf8');

const match = SNIPPET_EXTRACT.exec(content);
if (match) {
content = match[1];
}

if (content[content.length - 1] === '\n') {
content = content.slice(0, -1);
}

return content;
};

const replaceSnippets = (filename, fileString, regex, wrapWithTag = true) => {
const match = fileString.match(regex);
if (match) {
match.forEach(el => {
let name = el.split('=')[1].slice(0, wrapWithTag ? -1 : -2),
dirPath = getDirPath(filename),
filenames = fs.readdirSync(dirPath),
code = '';

const indent = countIndent(fileString, fileString.indexOf(el));

let languageName;
filenames.forEach(file => {
if (file.split('.')[0] === name && file.substr(-8) !== 'test.yml') {
const fileContent = getSnippetContent(dirPath, file);
const lng = file.split('.')[1];

languageName = lng === 'js' ? 'javascript' : lng;
code += wrapWithTag
? '``` ' + languageName + '\n' + fileContent + '\n```\n'
: fileContent;
}
});

const markdown = getMarkdown(code, languageName, indent, wrapWithTag);
fileString = fileString.replace(
indent.char.repeat(indent.count) + el,
markdown
);
});
}
return fileString;
};

module.exports = {
process(filename, data) {
let self = this,
fileString = data.contents.toString(),
match = false;

let newFileString = replaceSnippets(
filename,
fileString,
SNIPPET_INOTHER_REGEX,
false
);
newFileString = replaceSnippets(filename, newFileString, SNIPPET_REGEX);
if (newFileString !== fileString) {
fileString = newFileString;
match = true;
}

return {
has_code_example : (match) ? true : false,
has_code_example: match,
fileContent: new Buffer(fileString)
};
},

report (args) {
report(args) {
console.log(color.yellow('[TO-DO] =>'), args);
}
};
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
// Loads the Kuzzle SDK modules
const {
Kuzzle,
WebSocket
} = require('kuzzle-sdk');
const { Kuzzle, WebSocket } = require('kuzzle-sdk');

// Instantiates a Kuzzle client with the WebSocket protocol
// Replace 'kuzzle' with your Kuzzle server hostname (e.g. 'localhost')
const kuzzle = new Kuzzle(
new WebSocket('kuzzle')
);
const kuzzle = new Kuzzle(new WebSocket('kuzzle'));

// Adds a listener to detect any connection problems
kuzzle.on('networkError', error => {
Expand All @@ -26,21 +21,24 @@ const run = async () => {
};

// Defines a callback invoked each time a notification is received
const callback = (notification) => {

if (notification.type === 'document' && notification.action === 'create') {
const {
_source: driver,
_id: driverId
} = notification.result;

console.log(`New driver ${driver.name} with id ${driverId} has B license.`);
kuzzle.disconnect();
const callback = notification => {
if (!(notification.type === 'document' && notification.action === 'create')) {
return;
}
const { _source: driver, _id: driverId } = notification.result;
console.log(
`New driver ${driver.name} with id ${driverId} has B license.`
);
kuzzle.disconnect();
};

// Subscribes to document notifications using the above filter
await kuzzle.realtime.subscribe('nyc-open-data', 'yellow-taxi', filter, callback);
await kuzzle.realtime.subscribe(
'nyc-open-data',
'yellow-taxi',
filter,
callback
);

console.log('Successfully subscribed to document notifications!');
} catch (error) {
Expand Down
Loading