@rshirohara/rekurke-repixe
Version:
rekurke plugin that turns kakuyomu novel format into pixiv novel format to support repixe.
94 lines (93 loc) • 2.67 kB
JavaScript
export function convertRoot(tree, options) {
return {
type: "root",
children: [...tree.children]
.map((node) => {
switch (node.type) {
case "paragraph": {
return convertParagraph(node, options);
}
case "paragraphMargin": {
return convertParagraphMargin(node, options);
}
case "text": {
return convertText(node, options);
}
case "ruby": {
return convertRuby(node, options);
}
case "emphasis": {
return convertEmphasis(node, options);
}
case "break": {
return convertBreak(node, options);
}
default: {
return undefined;
}
}
})
.filter((node) => node !== undefined),
};
}
// FlowContent
function convertParagraph(tree, options) {
return {
type: "paragraph",
children: [...tree.children]
.map((node) => {
switch (node.type) {
case "text": {
return convertText(node, options);
}
case "ruby": {
return convertRuby(node, options);
}
case "emphasis": {
return convertEmphasis(node, options);
}
case "break": {
return convertBreak(node, options);
}
default: {
return undefined;
}
}
})
.filter((node) => node !== undefined),
};
}
function convertParagraphMargin(_tree, _options) {
return undefined;
}
// PhrasingContent
function convertText(tree, _) {
return {
type: "text",
value: tree.value,
};
}
function convertRuby(tree, _options) {
return {
type: "ruby",
value: tree.value,
ruby: tree.ruby,
};
}
function convertEmphasis(tree, options) {
if (options.preserveUnmatchedSyntax) {
return { type: "text", value: `《《${tree.value}》》` };
}
if (options.convertEmphasisToRuby.enable) {
return {
type: "ruby",
value: tree.value,
ruby: options.convertEmphasisToRuby.character[0]?.repeat(tree.value.length) ??
"",
};
}
return { type: "text", value: tree.value };
}
function convertBreak(_tree, _options) {
return { type: "break" };
}