feat(markdown): support markdown region inclusion

pull/3978/head
s-elo 2 years ago committed by Divyansh Singh
parent b694900e69
commit 52ee936f1a

@ -66,7 +66,7 @@ function testLine(
)
}
function findRegion(lines: Array<string>, regionName: string) {
export function findRegion(lines: Array<string>, regionName: string) {
const regionRegexps = [
/^\/\/ ?#?((?:end)?region) ([\w*-]+)$/, // javascript, typescript, java
/^\/\* ?#((?:end)?region) ([\w*-]+) ?\*\/$/, // css, less, scss

@ -2,6 +2,7 @@ import fs from 'fs-extra'
import matter from 'gray-matter'
import path from 'path'
import { slash } from '../shared'
import { findRegion } from '../markdown/plugins/snippet'
export function processIncludes(
srcDir: string,
@ -11,17 +12,33 @@ export function processIncludes(
): string {
const includesRE = /<!--\s*@include:\s*(.*?)\s*-->/g
const rangeRE = /\{(\d*),(\d*)\}$/
const regionRE = /(#[\w-]+)/
return src.replace(includesRE, (m: string, m1: string) => {
if (!m1.length) return m
const range = m1.match(rangeRE)
const region = m1.match(regionRE)
range && (m1 = m1.slice(0, -range[0].length))
region && (m1 = m1.slice(0, -region[0].length))
const atPresent = m1[0] === '@'
try {
const includePath = atPresent
? path.join(srcDir, m1.slice(m1[1] === '/' ? 2 : 1))
: path.join(path.dirname(file), m1)
let content = fs.readFileSync(includePath, 'utf-8')
if (region) {
const [ regionName ] = region
const lines = content.split(/\r?\n/);
const regionLines = findRegion(lines, regionName.replace('#', ''))
content = lines.slice(
regionLines?.start || undefined,
regionLines?.end || undefined
).join("\n")
}
if (range) {
const [, startLine, endLine] = range
const lines = content.split(/\r?\n/)

Loading…
Cancel
Save