Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
TypeScript API to generate a static website.
Install the ssg-api as a project dependency:
npm install --save ssg-api
Then import the required types to implement your own SSG code:
import {Ssg, SsgContextImpl, SsgConfig} from "ssg-api"
const config: SsgConfig = {outDir: "out"}
const context = new SsgContextImpl("fr")
new Ssg(config)
.add(someStep)
.add(otherStep)
.start(context) // Start the generation in outDir
.then(result => console.log("Completed", result))
.catch(err => console.error(err, context.inputFile.name, "=>", context.outputFile.name))
No that this is a native ESM package so it may not be supported by all test frameworks out of the box.
For instance, Jest will require some specifics in its jest.config.js
to transform the package code (here as ts-jest config):
/** @type {import("ts-jest/dist/types").InitialOptionsTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
transformIgnorePatterns: ["node_modules/(?!ssg-api)"],
transform: {
"^.+\\.[tj]s$": "ts-jest"
}
}
The Ssg execute a number of Steps, sequentially. A Step can do anything, but here are some pre-defined steps:
Example:
import {SsgConfig, SsgContextImpl, Ssg, ContentStep, CopyStep} from "ssg-api"
const config: SsgConfig = {outDir: "out"}
const context = new SsgContextImpl("fr")
new Ssg(config)
.add(new ContentStep(contentConfigs, outputFunc))
.add(dir1SubdirectoriesStep)
.add(dir2SubdirectoriesStep)
.add(...anArrayOfSteps)
.add(new CopyStep(copiesToDo))
.start(context) // Start the generation
.then(result => console.log("Completed", result))
.catch(err => console.error(err, context.inputFile.name, "=>", context.outputFile.name))
You can create your own steps by implementing the SsgStep interface.
A SsgContext is provided to Ssg methods to carry information about :
locale
(s)inputFile
that has been readoutputFile
that is about to be writtengetVar()
) that may have been set by current or previous replacements (through setVar()
).It also provides utility logging methods (log()
/warn()
/error()
/debug()
) and a clone()
method.
You can create your own context by implementing the SsgContext interface (typically to provide custom info to custom steps).
A ContentStep is parameterized by its ContentStepConfig
s.
Each of these configs specifies how the ContentStep will:
roots
.replacements
on each of them, until the file doesn't change anymore.outputSpec
.ContentStep replacements are classes implementing the ReplaceCommand
interface.
A number of concrete and abstract predefined replace commands are available. (you'll find a number of SSI ones because RR0 used to rely on them).
${varName}
) in the input files.SsiIfReplaceCommand
or displayed by the SsiEchoVarCommand
)..htaccess
sections.and others in the repository.
const contentConfigs: ContentStepConfig[] = [
{ // A content config that converts .htaccess to netlify.toml format
roots: [".htaccess"],
replacements: [new HtAccessToNetlifyConfigReplaceCommand("https://rr0.org/")],
getOutputFile(context: SsgContext): FileInfo {
return getFileInfo(context, "netlify.toml", "utf-8")
}
},
{ // A content config that replaces parts in roots
roots: ["index.html", "404.html", "pages/**/*.html"],
replacements: [
new SsiIncludeReplaceCommand(),
new TitleReplaceCommand(),
new StringEchoVarReplaceCommand("mail"),
new AngularExpressionReplaceCommand(),
new SsiEchoVarReplaceCommand("copyright"),
new SsiIfReplaceCommand(),
new SsiSetVarReplaceCommand("title", (match: string, ...args: any[]) => `<title>${args[0]}</title>`),
new SsiLastModifiedReplaceCommand(context.time.options),
new AuthorReplaceCommand(),
new HtmlTagReplaceCommand("time", new MyTimeReplacerFactory()),
new ClassRegexReplaceCommand("people", new MyPeopleClassReplacerFactory()),
new ClassRegexReplaceCommand("part(.?)", new MyPartXReplacerFactory()),
new LinkReplaceCommand(),
new AnchorReplaceCommand("https://my.base.url/")
],
getOutputFile(context: SsgContext): FileInfo {
return context.inputFile // Under output root, I don't want to change the file path.
}
}
]
new Ssg(config)
.add(new ContentStep(contentConfigs, outputFunc))
.start(context) // Start the generation
.then(result => console.log("Completed", result))
.catch(err => console.error(err, context.inputFile.name, "=>", context.outputFile.name))
ssg-api has been developed to generate the RR0 website, soits repository is a good place to find examples:
<time>yyyy-mm-dd hh:mm</time>
tags with links to a page about that very date.ClassDomReplaceCommand("place", new PlaceReplacerFactory(placeService))
to replace <span class="place">Paris (France)</span>
tags with a clickable tag to display the map of the mentioned place.SsgContextImpl
to add access to locale-specific messages and time context.FAQs
Static Site Generation TypeScript API
The npm package ssg-api receives a total of 174 weekly downloads. As such, ssg-api popularity was classified as not popular.
We found that ssg-api demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.