Skip to content

Commit

Permalink
use standard code style
Browse files Browse the repository at this point in the history
  • Loading branch information
zeke committed Apr 24, 2015
1 parent 7a13fbd commit 6195df7
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 191 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ The [content.json](/content.json) file is served publicly at `/content.json`
with CORS support, allowing browsers on other domains to fetch all the npm
documentation and accompanying metadata with a single HTTP call.

## Tests

```sh
npm install
npm test
```

[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)

## Deployment

```sh
Expand Down
86 changes: 39 additions & 47 deletions bin/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,27 @@
// Walk the content directory looking for markdown files
// and build an object of all the pages and sections (directories)

var path = require("path")
var fs = require("fs")
var fmt = require("util").format
var strftime = require("strftime")
var cheerio = require("cheerio")
var marky = require("marky-markdown")
var frontmatter = require("html-frontmatter")
var _ = require("lodash")

var compact = _.compact
var uniq = _.uniq
var pluck = _.pluck
var merge = _.merge

var contentFile = path.resolve(__dirname, "../content.json")
var path = require('path')
var fs = require('fs')
var strftime = require('strftime')
var marky = require('marky-markdown')
var frontmatter = require('html-frontmatter')
var _ = require('lodash')

var merge = _.merge

var contentFile = path.resolve(__dirname, '../content.json')

// Flesh out the content object
var content = {
sections: require("../sections.json"),
sections: require('../sections.json'),
pages: []
}

// Walk around looking for pages
var emitter = require("walkdir")(path.resolve(__dirname, "../content/"))

emitter.on("file", function(filepath,stat){
var emitter = require('walkdir')(path.resolve(__dirname, '../content/'))

emitter.on('file', function (filepath, stat) {
// We only want markdown files
if (!filepath.match(/\.md$/)) return

Expand All @@ -38,57 +32,58 @@ emitter.on("file", function(filepath,stat){
heading: null,
section: null,
href: null,
filename: filepath.replace(/.*\/content\//, ""),
filename: filepath.replace(/.*\/content\//, ''),
modified: null,
modifiedPretty: null,
edit_url: "https://github.com/npm/npm/edit/master/doc/api/npm-bugs.md",
content: fs.readFileSync(filepath, "utf-8")
edit_url: 'https://github.com/npm/npm/edit/master/doc/api/npm-bugs.md',
content: fs.readFileSync(filepath, 'utf-8')
}

// Get modified date
page.modified = fs.statSync(filepath).mtime
page.modifiedPretty = strftime("%B %d, %Y", page.modified)
page.modifiedPretty = strftime('%B %d, %Y', page.modified)

// Look for HTML frontmatter
merge(page, frontmatter(page.content))

// Look for man-pagey frontmatter
var manPattern = new RegExp("^(.*) -- (.*)\n=+\n")
var manPattern = new RegExp('^(.*) -- (.*)\\n=+\\n')
if (page.content.match(manPattern)) {
var manHead = manPattern.exec(page.content)
page.heading = manHead[2]
page.content = page.content.replace(manHead[0], "")
page.content = page.content.replace(manHead[0], '')
}

// Titlecase some headings from the npm/npm docs
page.content = page.content
.replace(/## SYNOPSIS/g, "## Synopsis")
.replace(/## DESCRIPTION/g, "## Description")
.replace(/## CONFIGURATION/g, "## Configuration")
.replace(/## SEE ALSO/g, "## See Also")
.replace(/## SYNOPSIS/g, '## Synopsis')
.replace(/## DESCRIPTION/g, '## Description')
.replace(/## CONFIGURATION/g, '## Configuration')
.replace(/## SEE ALSO/g, '## See Also')

// Convert markdown to HTML
page.content = marky(page.content, {
sanitize: false, // allow script tags and stuff
prefixHeadingIds: false, // don't apply safe prefixes to h1/h2... DOM ids
prefixHeadingIds: false // don't apply safe prefixes to h1/h2... DOM ids
}).html()

// Infer section from top directory
if (page.filename.match(/\//))
page.section = page.filename.split("/")[0]
if (page.filename.match(/\//)) {
page.section = page.filename.split('/')[0]
}

// In what repository does this doc live?
if (["api", "cli", "files", "misc"].indexOf(page.section) > -1) {
page.edit_url = "https://github.com/npm/npm/edit/master/doc/" + page.filename
} else if (page.section === "policies") {
page.edit_url = "https://github.com/npm/policies/edit/master/" + path.basename(page.filename)
if (['api', 'cli', 'files', 'misc'].indexOf(page.section) > -1) {
page.edit_url = 'https://github.com/npm/npm/edit/master/doc/' + page.filename
} else if (page.section === 'policies') {
page.edit_url = 'https://github.com/npm/policies/edit/master/' + path.basename(page.filename)
} else if (page.section) {
page.edit_url = "https://github.com/npm/docs/edit/master/content/" + page.filename
page.edit_url = 'https://github.com/npm/docs/edit/master/content/' + page.filename
}

page.href = "/" + page.filename
.replace(/\/npm-/, "/")
.replace(/\.md$/, "")
page.href = '/' + page.filename
.replace(/\/npm-/, '/')
.replace(/\.md$/, '')

// Use href as title if not specified in frontmatter
if (!page.title) {
Expand All @@ -99,15 +94,12 @@ emitter.on("file", function(filepath,stat){

})


emitter.on("end",function(){

content.pages = _.sortBy(content.pages, function(page) {
return page.title.toLowerCase();
emitter.on('end', function () {
content.pages = _.sortBy(content.pages, function (page) {
return page.title.toLowerCase()
})

fs.writeFileSync(contentFile, JSON.stringify(content, null, 2))
console.log("Wrote %s", path.relative(process.cwd(), contentFile))

console.log('Wrote %s', path.relative(process.cwd(), contentFile))

})
89 changes: 43 additions & 46 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,106 +1,103 @@
var express = require("express")
var hbs = require("hbs")
var harp = require("harp")
var path = require("path")
var cors = require("cors")
var find = require("lodash").find
var merge = require("lodash").merge
var isNumber = require("lodash").isNumber
var sortBy = require("lodash").sortBy
var any = require("lodash").any
var suggest = require(__dirname + "/lib/suggestions")
var redirects = require(__dirname + "/lib/redirects")
var express = require('express')
var hbs = require('hbs')
var harp = require('harp')
var path = require('path')
var cors = require('cors')
var find = require('lodash').find
var merge = require('lodash').merge
var sortBy = require('lodash').sortBy
var any = require('lodash').any
var suggest = require(__dirname + '/lib/suggestions')
var redirects = require(__dirname + '/lib/redirects')

// Load section and page data
var content = require(path.resolve(__dirname, "content.json"))
var content = require(path.resolve(__dirname, 'content.json'))

// Copy pages into their sections and sort them by order or title
content.sections.forEach(function(section){
section.pages = content.pages.filter(function(page) {
return section.id == page.section
content.sections.forEach(function (section) {
section.pages = content.pages.filter(function (page) {
return section.id === page.section
})

// Sort section pages if any of the pages have an `order` property
// Pages without the order property will come last
if (any(section.pages, 'order')) {
section.pages = sortBy(section.pages, function(page) {
section.pages = sortBy(section.pages, function (page) {
return Number(page.order || 10000)
})
}
})

var lite = merge({}, content)
lite.pages = lite.pages.map(function(page){
lite.pages = lite.pages.map(function (page) {
delete page.content
return page
})

// Configure Express
var app = module.exports = express()
app.set("view engine", "hbs")
app.set("port", (process.env.PORT || 5000))
app.use(express.static(__dirname + "/public"))
app.use(harp.mount(__dirname + "/public"))
hbs.registerPartials(__dirname + "/views/partials")
hbs.registerHelper("equal", require("handlebars-helper-equal"))

app.get("/", function(req, res) {
res.render("index", {
app.set('view engine', 'hbs')
app.set('port', (process.env.PORT || 5000))
app.use(express.static(__dirname + '/public'))
app.use(harp.mount(__dirname + '/public'))
hbs.registerPartials(__dirname + '/views/partials')
hbs.registerHelper('equal', require('handlebars-helper-equal'))

app.get('/', function (req, res) {
res.render('index', {
content: content,
recentlyUpdatedPages: [],
pageId: "index"
pageId: 'index'
})
})


app.get("/_monitor/ping", cors(), function(req, res) {
app.get('/_monitor/ping', cors(), function (req, res) {
res.status(200).send('pong')
})

app.get("/_monitor/status", cors(), function(req, res) {
app.get('/_monitor/status', cors(), function (req, res) {
res.json({
name: "docs",
name: 'docs',
pid: process.pid,
uptime: process.uptime(),
rss: process.memoryUsage(),
rss: process.memoryUsage()
})
})

app.get("/content.json", cors(), function(req, res) {
app.get('/content.json', cors(), function (req, res) {
res.json(content)
})

app.get("/content.lite.json", cors(), function(req, res) {
app.get('/content.lite.json', cors(), function (req, res) {
res.json(lite)
})

app.get("/all", function(req, res) {
res.render("multi", {
app.get('/all', function (req, res) {
res.render('multi', {
content: content,
heading: "All Docs"
heading: 'All Docs'
})
})

app.get("/*", function(req, res) {

app.get('/*', function (req, res) {
if (req.path in redirects) {
return res.redirect(301, redirects[req.path])
}

var page = find(content.pages, function(page) {
var page = find(content.pages, function (page) {
return page.href === req.path
})

if (!page) {
return res.status(404).render("404", {
return res.status(404).render('404', {
url: req.url,
pageId: "fourohfour",
pageId: 'fourohfour',
content: content,
suggestions: suggest(req.path, content.pages)
})
}

res.render("page", {
res.render('page', {
page: page,
content: content
})
Expand All @@ -110,7 +107,7 @@ app.get("/*", function(req, res) {
// This module.parent thing allows us to test the server using
// supertest without unnecessarily firing up the server.
if (!module.parent) {
app.listen(app.get("port"), function() {
console.log("Running at localhost:" + app.get("port"))
app.listen(app.get('port'), function () {
console.log('Running at localhost:' + app.get('port'))
})
}
6 changes: 3 additions & 3 deletions lib/redirects.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
"/enterprise/whitelist": "/enterprise/mirroring",
"/misc/index": "/",
"/package.json": "/files/package.json",
'/enterprise/whitelist': '/enterprise/mirroring',
'/misc/index': '/',
'/package.json': '/files/package.json'
}
17 changes: 8 additions & 9 deletions lib/suggestions.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
var distance = require("leven")
var sortBy = require("lodash").sortBy

module.exports = function(input, pages) {
var distance = require('leven')
var sortBy = require('lodash').sortBy

module.exports = function (input, pages) {
var maxDistance = 3
var maxResults = 3

var suggestions = pages.filter(function(page) {
var suggestions = pages.filter(function (page) {
return distance(input, page.href) <= maxDistance
})

suggestions = sortBy(suggestions, function(page) {
suggestions = sortBy(suggestions, function (page) {
return distance(input, page.href)
}).slice(0,maxResults)
}).slice(0, maxResults)

suggestions = suggestions.map(function(page) {
page.hrefWithoutSlash = page.href.replace(/^\//, "")
suggestions = suggestions.map(function (page) {
page.hrefWithoutSlash = page.href.replace(/^\//, '')
return page
})

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"build": "bin/build.sh",
"pretest": "npm run build",
"prestart": "npm run build",
"test": "mocha"
"test": "standard && mocha"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -48,6 +48,7 @@
"cheerio": "^0.18.0",
"mocha": "^1.21.4",
"nodemon": "^1.3.5",
"standard": "^3.7.1",
"supertest": "^0.14.0"
}
}
Loading

0 comments on commit 6195df7

Please sign in to comment.