67 lines
2.1 KiB
JavaScript
67 lines
2.1 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const promise_1 = __importDefault(require("../utils/promise"));
|
|
const error_1 = __importDefault(require("../utils/error"));
|
|
const lookupStructureFile_1 = __importDefault(require("./lookupStructureFile"));
|
|
const path_1 = __importDefault(require("path"));
|
|
/**
|
|
Parse a ParsableFile using a specific method
|
|
|
|
@param {FS} fs
|
|
@param {ParsableFile} file
|
|
@param {string} type
|
|
@return {Promise<Array<String, List|Map>>}
|
|
*/
|
|
function parseFile(fs, file, type) {
|
|
const filepath = file.getPath();
|
|
const parser = file.getParser();
|
|
if (!parser) {
|
|
return promise_1.default.reject(error_1.default.FileNotParsableError({
|
|
filename: filepath
|
|
}));
|
|
}
|
|
const baseDirectory = path_1.default.dirname(filepath);
|
|
return fs
|
|
.readAsString(filepath)
|
|
.then((content) => {
|
|
if (type === "readme") {
|
|
return parser.parseReadme(content, { baseDirectory });
|
|
}
|
|
else if (type === "glossary") {
|
|
return parser.parseGlossary(content, { baseDirectory });
|
|
}
|
|
else if (type === "summary") {
|
|
return parser.parseSummary(content, { baseDirectory });
|
|
}
|
|
else if (type === "langs") {
|
|
return parser.parseLanguages(content, { baseDirectory });
|
|
}
|
|
else {
|
|
throw new Error(`Parsing invalid type "${type}"`);
|
|
}
|
|
})
|
|
.then((result) => {
|
|
return [file, result];
|
|
});
|
|
}
|
|
/**
|
|
Parse a structure file (ex: SUMMARY.md, GLOSSARY.md).
|
|
It uses the configuration to find the specified file.
|
|
|
|
@param {Book} book
|
|
@param {string} type: one of ["glossary", "readme", "summary"]
|
|
@return {Promise<List|Map>}
|
|
*/
|
|
function parseStructureFile(book, type) {
|
|
const fs = book.getContentFS();
|
|
return (0, lookupStructureFile_1.default)(book, type).then((file) => {
|
|
if (!file)
|
|
return [undefined, undefined];
|
|
return parseFile(fs, file, type);
|
|
});
|
|
}
|
|
exports.default = parseStructureFile;
|