Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
render(tokens, idx) {
const { escapeHtml } = instance.escapeHtml
const attrs = { image: '', caption: '', citation: '' }
let result = ''
if (tokens[idx].nesting === 1) {
// opening tag
let matches
while ((matches = attrsRe.exec(tokens[idx].info.trim())) !== null) {
// eslint-disable-next-line prefer-destructuring
attrs[matches[1]] = matches[2]
}
if (!attrs.image && !attrs.caption) {
log.error(`[${context.fileName}.md] Malformed directive.`)
result = ''
} else if (!attrs.image && attrs.caption) {
const captions = attrs.caption.split('|').map(_ => _.trim())
const citations = attrs.citation.split('|').map(_ => _.trim())
result = [
'<section class="epigraph chapter">',
'<section class="subchapter">',
captions
.map(
(caption, idx2) =>
`<div class="pullquote full-width">
<p>${escapeHtml(caption)}</p>
${
citations[idx2]
? `<cite>—${escapeHtml(
citations[idx2]</cite></div></section></section>
function prepare({ token, match, instance, context, fileName, lineNumber }) {
const [, type, id, attrs] = match
const figureId = htmlId(id)
const caption = token.children ? instance.renderInline(token.children) : ''
const comment = Html.comment(
`START: figure:${type}#${figureId}; ${fileName}:${lineNumber}`
)
const attrsObject = attributesObject(attrs, type, { fileName, lineNumber })
const asset = state.src.images(attrsObject.source)
const mediaType =
(type.indexOf('-') && type.substring(0, type.indexOf('-'))) || type
// make sure image exists
if (!fs.existsSync(asset)) log.error(`Image not found [${asset}]`)
// then get the dimensions
const { width, height } = sizeOf(asset)
return {
width,
height,
caption,
comment,
context,
attrsObject,
asset,
mediaType,
id: figureId,
}
}
if (coverListedInMetadata) {
// TODO: fixme, for generated covers
// @issue: https://github.com/triplecanopy/b-ber/issues/208
this.coverEntry = coverListedInMetadata.replace(/_jpg$/, '.jpg')
log.info('cover verify image [%s]', this.coverEntry)
// there's a reference to a cover image so we create a cover.xhtml file
// containing an SVG-wrapped `image` element with the appropriate cover
// dimensions, and write it to the `text` dir.
// check that the cover image file exists, throw if not
this.coverImagePath = state.src.images(this.coverEntry)
if (!fs.existsSync(this.coverImagePath)) {
log.error('cover image listed in metadata.yml cannot be found [%s]', this.coverImagePath)
}
return this.generateCoverXHTML()
} // end if cover exists
// if there's no cover referenced in the metadata.yml, we create one
// that displays the book's metadata (title, generator version, etc)
// and add it to metadata.yml
log.info('cover generated image [%s]', this.coverEntry)
const coverMetadata = { term: 'cover', value: this.coverEntry }
state.metadata.add(coverMetadata)
this.metadata = { ...coverMetadata, ...this.metadata, ...metadata }
return this.removeDefaultCovers()
let theme
if (defaultThemes[name]) {
theme = defaultThemes[name]
} else {
// check for both vendor and user paths
const userPath = path.resolve(state.config.themes_directory, name)
const vendorPath = path.resolve('node_modules', name)
// prefer the user path if both exist, since a user might've
// copied over the vendor theme and is changing it
const themePath = fs.existsSync(userPath) ? userPath : fs.existsSync(vendorPath) ? vendorPath : null
if (themes.indexOf(name) < 0 || !themePath) {
log.error(`Theme [${name}] is not installed`)
}
theme = require(themePath)
}
return createProjectThemeDirectory(name)
.then(() => copyThemeAssets(theme))
.then(() => updateConfig(name))
.then(() => !force && log.notice('Updated theme to', name))
.catch(log.error)
}
}
} else {
sources = [source]
sourceElements = createRemoteMediaSource(sources)
}
state.add('remoteAssets', source)
} else if (validateLocalMediaSource(source, mediaType)) {
sources = media.filter(a => toAlias(a) === source)
sourceElements = createLocalMediaSources(sources)
}
if (!sources.length && type !== 'iframe' && type !== 'iframe-inline') {
err = new Error(
`bber-directives: Could not find matching [${mediaType}] with the basename [${source}]`
)
log.error(err)
}
delete attrsObject.source // otherwise we get a `src` tag on our video element
if (mediaType === 'audio') {
delete attrsObject.poster // invalid attr for audio elements
}
if (mediaType === 'video') {
// TODO: needs to be done async
//
// add aspect ratio class name
// const aspecRatioClassName = isHostedRemotely(source)
// ? getVideoAspectRatio()
// : getVideoAspectRatio(path.resolve(state.src.media(head(sources))))
aspecRatioClassName = 'video--16x9'
export const validatePosterImage = (_asset, type) => {
const asset = state.src.images(_asset)
const isInlineMedia = /inline/.test(type)
if (!fs.existsSync(asset)) {
if (isInlineMedia) {
log.error(
'bber-directives: inline media directives requires a [poster] attribute, aborting'
)
} else {
log.error(`bber-directives: Poster image for [${type}] does not exist`)
}
}
return asset
}
add = (prop, value) => {
const prevValue = get(this, prop)
if (isArray(prevValue)) {
set(this, prop, [...prevValue, value])
} else if (isPlainObject(prevValue)) {
set(this, prop, { ...prevValue, value })
} else if (typeof prevValue === 'string') {
set(this, prop, `${prevValue}${value}`)
} else {
log.error(`Cannot add [${value}] to [state.${prop}]`)
}
}
function ensureEnvVars() {
const {
AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY,
BBER_BUCKET_REGION,
} = process.env
if (!AWS_ACCESS_KEY_ID || !AWS_SECRET_ACCESS_KEY || !BBER_BUCKET_REGION) {
log.error(
'[AWS_ACCESS_KEY_ID], [AWS_SECRET_ACCESS_KEY] and [BBER_BUCKET_REGION] must be set to deploy the project'
)
}
const configFile = path.resolve(cwd, 'config.yml')
const config = YamlAdaptor.load(configFile)
const { bucket_url: bucketURL } = config
if (!bucketURL) {
log.error('[bucketURL] must be set in config.yml to deploy the project')
}
return { bucketURL, awsRegion: BBER_BUCKET_REGION }
}
createFile({ markdownDir, metadata }) {
const frontmatter = `---\n${Object.entries(metadata).reduce(
(acc, [k, v]) => (v ? acc.concat(`${k}: ${v}\n`) : acc),
''
)}---\n`
const { title } = metadata
const fileName = `${title.replace(/[^a-z0-9_-]/gi, '-')}.md`
const filePath = path.join(markdownDir, fileName)
if (fs.existsSync(filePath))
log.error(`_markdown${path.sep}${fileName} already exists, aborting`)
return fs.writeFile(filePath, frontmatter).then(() => ({ fileName }))
}
'opf [%s] was not declared in the TOC. Adding [%s] to [%s]',
name,
name,
path.basename(tocFile)
)
)
}
if (redundant.length) {
let message =
'Files declared in the TOC do not exist in the _markdown directory'
message += 'The following entries must be removed manually from '
message += `[${path.basename(tocFile)}]:`
message += redundant.map(name => `[${name}]`).join('\n')
log.error(message)
}
return Promise.all(promises)
}