Skip to content

Commit

Permalink
Fix the problen of miss "\n"
Browse files Browse the repository at this point in the history
  • Loading branch information
Arylo committed Jan 4, 2018
1 parent e10ce19 commit ddd85ef
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 27 deletions.
51 changes: 30 additions & 21 deletions lib/transform.ts
Expand Up @@ -2,7 +2,6 @@ import npm = require("./npm");
import github = require("./github");
import { isArray } from "util";
const parse = require("markdown-to-ast").parse;
const toMd = require("ast-to-markdown");

interface IBlock {
type: string;
Expand All @@ -21,42 +20,52 @@ export = (content: string) => {
const ast: IBlock = parse(content);

const tranChildren = (block: IBlock) => {
let text = block.raw;

if (block.children && isArray(block.children)) {
let lastLine = block.loc.start.line;
let newRaw = "";
for (let b of block.children) {
b = tranChildren(b);
for (let bi = 0; bi < block.children.length; bi++) {
const b = block.children[bi];
block.children[bi] = tranChildren(b);
}
for (let b of block.children) {
const startLine = block.loc.start.line;
const endLine = block.loc.end.line;
if (startLine === lastLine) {
newRaw += b.raw;
// Supply \n
for (let bi = 0; bi < block.children.length; bi++) {
if (bi === 0) {
continue;
}
const b = block.children[bi];
const pb = block.children[bi - 1];
const startLine = b.loc.start.line;
const endLine = pb.loc.end.line;
if ((endLine - startLine - 1) <= 0) {
continue;
}
for (let i = 0; i < (startLine - lastLine); i++) {
newRaw += "\n";
for (let i = 0; i < (endLine - startLine); i++) {
b.raw = "\\n" + b.raw;
}
newRaw += b.raw;
lastLine = endLine;
}
block.raw = newRaw;
// Replace String
for (let bi = block.children.length - 1; bi >= 0; bi--) {
const b = block.children[bi];
const text = block.raw;
const strs = [
text.slice(0, b.range[0] - block.range[0]), // Start
b.raw, // Mid
text.slice(b.range[1] - block.range[0]) // End
];
block.raw = strs.join("");
}
return block;
}
if (block.type === "CodeBlock" || block.type === "Code") {
return block;
}

// Process
let text = block.raw;
text = npm.process(text);
text = github.process(text);

block.raw = text;
if (block.value) {
block.value = text;
}
return block;
};

return toMd(tranChildren(ast)).replace(/(^\n|\n$)/g, "");
return tranChildren(ast).raw;
};
9 changes: 4 additions & 5 deletions package.json
@@ -1,20 +1,20 @@
{
"name": "hexo-reslink",
"version": "1.1.2",
"version": "1.1.3",
"description": "Automatically create resource links",
"main": "index.js",
"scripts": {
"clean": "rm -rf dist",
"tsc": "tsc",
"build": "npm run tsc",
"pretest": "npm run clean&&npm run build",
"pretest": "npm run clean&&npm run build&&cp -r test dist",
"test-spec": "mocha",
"test-cov": "nyc npm run test-spec && nyc report",
"test": "npm run test-cov",
"report-coverage": "cat ./coverage/lcov.info | coveralls"
},
"files": [
"dist",
"dist/lib",
"lib",
"README.md",
"LICENSE",
Expand Down Expand Up @@ -56,7 +56,6 @@
]
},
"dependencies": {
"ast-to-markdown": "^0.2.0",
"markdown-to-ast": "^6.0.2"
"markdown-to-ast": "6.0.2"
}
}
7 changes: 7 additions & 0 deletions test/data/raw.md
@@ -0,0 +1,7 @@
# Test Simple

Do you know the `hexo`(github:hexo/hexo)(npm:hexo)? I love the blog framework. So i code one plugin for it. This is github:hexo-reslink.

The Plugin can genertor resoure's URL into post. eg. `npm:hexo-reslink` to `[hexo-reslink](https://www.npmjs.com/package/hexo-reslink)`.

Have make fun~
7 changes: 7 additions & 0 deletions test/data/result.md
@@ -0,0 +1,7 @@
# Test Simple

Do you know the `hexo`([hexo/hexo](https://github.com/hexo/hexo "Github Resource: hexo/hexo"))([hexo](https://www.npmjs.com/package/hexo "Node Library: hexo"))? I love the blog framework. So i code one plugin for it. This is [hexo-reslink](https://github.com/hexo-reslink "Github Resource: hexo-reslink").

The Plugin can genertor resoure's URL into post. eg. `npm:hexo-reslink` to `[hexo-reslink](https://www.npmjs.com/package/hexo-reslink)`.

Have make fun~
10 changes: 9 additions & 1 deletion test/transform.spec.ts
@@ -1,8 +1,9 @@
import transform = require("../lib/transform");
import fs = require("fs");

describe('Transform Process', () => {

it('Default', () => {
it('One Line', () => {
const raw = `The npm:hexo is a very nice framework. Its homepage is github:hexojs/hexo.`;
const result = `The [hexo](https://www.npmjs.com/package/hexo "Node Library: hexo") is a very nice framework. Its homepage is [hexojs/hexo](https://github.com/hexojs/hexo "Github Resource: hexojs/hexo").`;
transform(raw).should.be.equal(result);
Expand All @@ -18,4 +19,11 @@ describe('Transform Process', () => {
transform(raw).should.be.equal(raw);
});

it("Words", () => {
const fsOpts = { encoding: "utf-8" };
const raw = fs.readFileSync(`${__dirname}/data/raw.md`, fsOpts);
const result = fs.readFileSync(`${__dirname}/data/result.md`, fsOpts);
transform(raw).should.be.equal(result);
});

});

0 comments on commit ddd85ef

Please sign in to comment.