parse json to render apple developer documentation
parent
195afdd469
commit
2ef1d7b784
25
index.js
25
index.js
|
@ -3,6 +3,7 @@ const turndown = require('turndown');
|
|||
const { Readability } = require('@mozilla/readability');
|
||||
const JSDOM = require('jsdom').JSDOM;
|
||||
const common_filters = require('./url_to_markdown_common_filters');
|
||||
const apple_dev_parser = require('./url_to_markdown_apple_dev_docs.js');
|
||||
const table_to_markdown = require('./html_table_to_markdown.js');
|
||||
const validURL = require('@7c/validurl');
|
||||
const express = require('express');
|
||||
|
@ -14,6 +15,8 @@ const app = express();
|
|||
|
||||
const service = new turndown();
|
||||
|
||||
const apple_dev_prefix = "https://developer.apple.com";
|
||||
|
||||
const rateLimiter = rateLimit({
|
||||
windowMs: 30 * 1000,
|
||||
max: 5,
|
||||
|
@ -42,7 +45,11 @@ app.get('/', (req, res) => {
|
|||
}
|
||||
if (url && validURL(url)) {
|
||||
send_headers(res);
|
||||
read_url(url, res, inline_title, ignore_links);
|
||||
if (url.startsWith(apple_dev_prefix)) {
|
||||
read_apple_url(url, res, inline_title, ignore_links);
|
||||
} else {
|
||||
read_url(url, res, inline_title, ignore_links);
|
||||
}
|
||||
} else {
|
||||
res.status(400).send("Please specify a valid url query parameter");
|
||||
}
|
||||
|
@ -113,6 +120,22 @@ function read_url(url, res, inline_title, ignore_links) {
|
|||
});
|
||||
}
|
||||
|
||||
function read_apple_url(url, res, inline_title, ignore_links) {
|
||||
//TODO: currently ignores the flags inline_title and ignore_links
|
||||
json_url = apple_dev_parser.dev_doc_url(url);
|
||||
https.get(json_url,(apple_res) => {
|
||||
let body = "";
|
||||
apple_res.on("data", (chunk) => {
|
||||
body += chunk;
|
||||
});
|
||||
apple_res.on("end", () => {
|
||||
let json = JSON.parse(body);
|
||||
let markdown = apple_dev_parser.parse_dev_doc_json(json);
|
||||
res.send(markdown);
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
function format_tables(html, replacements) {
|
||||
const tables = html.match(/(<table[^>]*>(?:.|\n)*?<\/table>)/gi);
|
||||
if (tables) {
|
||||
|
|
|
@ -0,0 +1,119 @@
|
|||
module.exports = {
|
||||
|
||||
dev_doc_url: function (url) {
|
||||
|
||||
if (url.endsWith('/')) {
|
||||
url = url.slice(0, -1)
|
||||
}
|
||||
|
||||
let parts = url.split("/");
|
||||
|
||||
let json_url = "https://developer.apple.com/tutorials/data";
|
||||
|
||||
for (let i=3; i<parts.length;i++) {
|
||||
json_url += "/" + parts[i];
|
||||
}
|
||||
json_url += ".json";
|
||||
|
||||
return json_url;
|
||||
|
||||
},
|
||||
|
||||
parse_dev_doc_json: function (json) {
|
||||
let text = "";
|
||||
if (typeof json.primaryContentSections !== 'undefined') {
|
||||
text += this.process_sections(json.primaryContentSections);
|
||||
} else if (typeof json.sections !== 'undefined') {
|
||||
text += this.process_sections(json.sections);
|
||||
}
|
||||
return text;
|
||||
},
|
||||
|
||||
process_sections: function (sections) {
|
||||
let text = "";
|
||||
|
||||
sections.forEach((section, i) => {
|
||||
|
||||
if (typeof section.kind !== 'undefined') {
|
||||
if (section.kind == 'declarations') {
|
||||
if (typeof section.declarations !== 'undefined') {
|
||||
section.declarations.forEach((declaration, i) => {
|
||||
|
||||
if (typeof declaration.tokens !== undefined) {
|
||||
token_text = "# ";
|
||||
declaration.tokens.forEach((token, i) => {
|
||||
token_text += token.text;
|
||||
});
|
||||
text += token_text;
|
||||
}
|
||||
|
||||
if (typeof declaration.languages !== undefined) {
|
||||
if (declaration.languages.length) {
|
||||
language_text = "\nLanguages: " + declaration.languages.join(', ');
|
||||
text += " "+language_text;
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof declaration.platforms !== undefined) {
|
||||
if (declaration.platforms.length) {
|
||||
platform_text = "\nPlatforms: " + declaration.platforms.join(', ');
|
||||
text += " "+platform_text;
|
||||
}
|
||||
}
|
||||
});
|
||||
text += "\n\n";
|
||||
}
|
||||
} else if (section.kind == 'content') {
|
||||
section.content.forEach((content, i) => {
|
||||
if (typeof content.type != 'undefined') {
|
||||
if (content.type == 'paragraph') {
|
||||
if (typeof content.inlineContent !== 'undefined') {
|
||||
inline_text = "";
|
||||
content.inlineContent.forEach((inline, i) => {
|
||||
if (typeof inline.type !== 'undefined') {
|
||||
if (inline.type == "text") {
|
||||
inline_text += inline.text;
|
||||
} else if (inline.type == "link") {
|
||||
inline_text += "["+inline.title+"]("+inline.destination+")";
|
||||
}
|
||||
}
|
||||
});
|
||||
text += inline_text + "\n\n";
|
||||
}
|
||||
} else if (content.type == 'codeListing') {
|
||||
code_text = "\n```\n";
|
||||
code_text += content.code.join("\n");
|
||||
code_text += "\n```\n\n";
|
||||
text += code_text;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof section.title !== 'undefined') {
|
||||
if (typeof section.kind !== 'undefined' && section.kind == 'hero') {
|
||||
text += "# " + section.title + "\n";
|
||||
} else {
|
||||
text += "## " + section.title;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (typeof section.content !== 'undefined') {
|
||||
section.content.forEach((sectionContent, i) => {
|
||||
if (typeof sectionContent.type !== 'undefined') {
|
||||
if (sectionContent.type == 'text') {
|
||||
text += sectionContent.text + "\n";
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
return text;
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue